Nathan,

You are description of the "fillMe" argument and the effect of the "fill()"
method is correct, but your assertion that "Java does use pass by reference"
is WRONG.

The following statement is absolutely true: "Java passes all arguments by
VALUE" (references listed below). 

When passing an Object it is the object reference that gets passed (not the
object itself) and it is passed by value. In other words, a copy of the
object reference is passed to the method. As you correctly stated, this
reference points the orginal object and can be used to modify the orginal
object thru its public mutator methods.

Tom

References:
"Exploring Java" 2nd edition, O'Reilly, Pg 143 "Argument Passing and
References"
"Java 2 Certification Study Guide", Sybex, Pg 17 "Argument Passing"



-----Original Message-----
From: Nathan Tenney [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 31, 2002 12:36 PM
To: JDJList
Subject: [jdjlist] Re: Java : pass by reference???


Ok, I didn't take the time to read all the messages in
this thread, so someone may have already answered this
to my satisfaction, but the 4 or 5 I did read had it
totally wrong.

Java does use pass by reference.  Think of the
variable fillMe in your fill methods has holding a
reference to another object.  At the beginning of the
method, it refers to the object you passed in when you
called fill.  However, when you used new, you changed
which object fillMe refers to, so any changes made to
fillMe at this point will not touch the original
object fillMe referred to.  NOTE: defining a String
explicitly (with "") is the same thing as using new.

The major problem with your example is that you used
immutable objects (String and Integer) as the objects
to refer to.  Immutable Objects are those that have no
methods for modifying the data that is stored in them.
 All you can do with them is create new Objects.  Try
this example with StringBuffer instead of String, and
replace fillMe = "test"; with fillMe.append(" test");
and you will see a difference in your output.

Hope that was easy to understand.

--- H Shankaranarayanan <[EMAIL PROTECTED]> wrote:
> class test
> {
>     /**
>      *  Description of the Method
>      *
>      *@param  fillMe  Description of the Parameter
>      */
>     public void fill(String fillMe)
>     {
>         fillMe = "test";
>     }
> 
> 
>     /**
>      *  Description of the Method
>      *
>      *@param  fillMe  Description of the Parameter
>      */
>     public void fill(Integer fillMe)
>     {
>         fillMe = new Integer(100);
>     }
> 
> 
>     /**
>      *  Description of the Method
>      *
>      *@param  args  Description of the Parameter
>      */
>     public static void main(String args[])
>     {
> 
>         try
>         {
>             test objTest = new test();
>             String testfill = new String("main");
>             objTest.fill(testfill);
>             System.out.println("Fill me result:" +
> testfill);
> 
>             Integer intFill = new Integer(200);
>             objTest.fill(intFill);
>             System.out.println("Fill me result:" +
> intFill);
> 
>         } catch (Exception e)
>         {
>             e.printStackTrace();
>         }
>     }
> }
> 
> 
> That is a sample program i wrote to test this fact.
> The result is dependent
> on scope of the variable.
> So wots this pass by reference concept that every
> text book around the world
> states about Java.
> 
> How does the pass by reference concept work anyways?
> 
> I might have missed something here. If i did i would
> appreciate if anyone
> told me wot is it that i did miss.
> 
> I was expecting this program to work otherwise but
> it does not.
> 
> --Shankar
> 
> 
> To change your membership options, refer to:
> http://www.sys-con.com/java/list.cfm


=====
----------------------------------
Nathan Tenney
Alumni Utah State University
[EMAIL PROTECTED]
----------------------------------

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com

To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm


THIS TRANSMISSION, INCLUDING ANY ATTACHMENTS OR FILES, 
CONTAINS AIRNET COMMUNICATIONS CORPORATION CONFIDENTIAL 
AND PROPRIETARY INFORMATION WHICH MAY BE OTHERWISE EXEMPT 
FROM DISCLOSURE. 
The information is intended to be for the exclusive use of the individual 
or entity named above. If you are not the intended recipient, 
be advised that any disclosure, copying, distribution or other use 
of this information is strictly prohibited.  If you have received this 
transmission in error, please notify us by telephone at 1-321-984-1990 or 
by email to [EMAIL PROTECTED] immediately and do not read, print 
or save this information in any manner. 

To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to