File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Question reg.  Pass By Value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question reg.  Pass By Value" Watch "Question reg.  Pass By Value" New topic
Author

Question reg. Pass By Value

vinodh rmahen
Greenhorn

Joined: Jul 22, 2008
Posts: 14
We all know that Java uses Pass by Value and not Pass by Reference, as illustrated by the output of the following snippet :


Output :
Before - a=10
After - a=10

This is as expected as we are trying to modify a copy & the original object is not affected.



But while trying the following snippet :


I was expecting the output to be :
Before - a.size()=0
After - a.size()=0

But the output is :
Before - a.size()=0
After - a.size()=1

Why this difference in behavior?
I could only think of the following :
In the first case the Object instance is copied and this copy is acted upon by function 'test', but in the second case a copy of the reference variable is sent thus we have two references to the same object instance(ArrayList, in this case) - is this what is actually happening?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

http://faq.javaranch.com/java/UseCodeTags

http://faq.javaranch.com/java/CallByReferenceVsCallByValue
As you can read there, the reference is copied. However, both references still point to the same object on the heap. If you change that object both references will see those changes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
vinodh rmahen
Greenhorn

Joined: Jul 22, 2008
Posts: 14
Rob Prime wrote:
However, both references still point to the same object on the heap. If you change that object both references will see those changes.


Thanks for mentioning about the code tags.
If this is the case, can you please explain why "a1+=10" doesn't modify the original Interger object (snippet 1).

Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

vinodh rmahen wrote:
If this is the case, can you please explain why "a1+=10" doesn't modify the original Interger object (snippet 1).


Because "+=" never modifies any Integer object -- a new object is created and the variable is changed to point to the new object.

In fact (and this is a little bit off topic), nothing ever modifies an Integer object, or a Float, Short, Long, Double, Character, Byte, or String; all of these classes are immutable, which means they provide no methods which can change their values in any way.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

a1 += 10 is shorthand for the following:

As you can see, a1 is reassigned, not modified as Ernest has explained.
vinodh rmahen
Greenhorn

Joined: Jul 22, 2008
Posts: 14
Thanks for explanations, all this time I assumed that the Object itself gets copied.
Looking at a higher level is this behavior not similar to 'Pass By Reference' feature of languages like C, C++? I understand that the Object reference is 'passed by value' but with respect to the Original Object it can still be modified within the function, thus I feel that the name 'Pass By Value' in Java is a little misleading .
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

vinodh rmahen wrote: I feel that the name 'Pass By Value' in Java is a little misleading .


Well, they could have said that objects are passed by reference, and primitives by value -- because primitives really are copied, and there's no way to pass (or even create) a "reference to primitive" in Java.

But instead the Java Language Spec authors chose to say that everything is passed by value, including object references. It's a simpler rule to remember, once you understand it correctly.
vinodh rmahen
Greenhorn

Joined: Jul 22, 2008
Posts: 14
Ernest Friedman-Hill wrote: It's a simpler rule to remember, once you understand it correctly.
Yes I totally agree, thanks once again for all the information.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question reg. Pass By Value
 
Similar Threads
Phantom Reference
Passing variable into methods
Doubts about Generics
parsing data and storing in the xml
Generic type casting