This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Unclear about pass by reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unclear about pass by reference" Watch "Unclear about pass by reference" New topic
Author

Unclear about pass by reference

Anand Kumar Loganathan
Greenhorn

Joined: Jul 01, 2012
Posts: 3


The output is

0,0
0,20

But i was expecting it to be


0,0
20,20 as both rt and intt are object references. Is my understanding correct?.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

No. Java is pass by value, so you can't do this.

You just passed an object reference by value. Pass by reference means passing variables by reference.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
The other thing that may not be obvious is that, within the method change(), the statement

assigns a value to an instance variable within the same rt object referenced by the caller . But the statement

creates a new Integer object to assign and assigns it to intt.

So the second statement changes the value of the object reference itself, and that does not affect the caller's reference.

You can read more about the creation of a new object for Integer by looking up "autoboxing".
Anand Kumar Loganathan
Greenhorn

Joined: Jul 01, 2012
Posts: 3
Thanks Stephen and Ralph!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unclear about pass by reference