File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unclear about pass by reference

 
Anand Kumar Loganathan
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 4840
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephen and Ralph!!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic