• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question reg. Pass By Value

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
vinodh rmahen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a1 += 10 is shorthand for the following:

As you can see, a1 is reassigned, not modified as Ernest has explained.
 
vinodh rmahen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic