Great answer, I lost a job interview over this very issue. They said Java does not pass by reference. Turns out it was a sweat shop anyway. My gain.
doug.. -----Original Message----- From: Nathan Tenney [mailto:[EMAIL PROTECTED]] Sent: Friday, May 31, 2002 11:36 AM 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 To change your membership options, refer to: http://www.sys-con.com/java/list.cfm
