• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing by value problem

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys given the following code:


I would have thought the result was something like 11 and 10 because yes we know that java passes by value but we're passing the reference to an object as first parameter, so the call should affect the original object, shouldn't it?
 
Henry Wong
author
Marshal
Pie
Posts: 22089
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:
I would have thought the result was something like 11 and 10 because yes we know that java passes by value but we're passing the reference to an object as first parameter, so the call should affect the original object, shouldn't it?


Java doesn't use "pass by reference" for objects. A copy of the reference variable is passed -- ie. it is "pass by value" of the reference variable. This means that changes to a mutable object itself will be seen, but changes to the reference will not.

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, what do you mean by "seen"?
 
Liutauras Vilda
Bartender
Pie
Posts: 2766
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:thanks, what do you mean by "seen"?


 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried an example to demonstrate changes to mutable object can be seen and changes to reference can't be
Output:
Person1 in main before method call: Sachin
Person1 in method: Sachin
Person1 in main after method call: Virat           // Here change can be seen

  • If I understood correctly in Jason's example a copy of the reference variable val1 is passed to method testInts, since Integer is an object so both val1 and obj refers to the same object of type Integer having value 5. On the very next line
  • here var is int type so boxing is done and assigned to the reference variable obj, now obj refers to object having value 10 but not to the object which had value 5.

    On next line obj++; I googled about It, since Integer is immutable this creates new object of Integer for this incremented value.

  • did you notice here space between postfix ++ operator gives compile time error
  • but in prefix It doesn't give CE and also doesn't even increment the value of val2 whilst passing to method. does that consider It like +val2 for positive value of val2 but what about another +?
     
    Jason Attin
    Ranch Hand
    Posts: 232
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks, well the new example does what I thought it would do, which is effectively change the value of the object, but the other one doesn't, meaning, in the first code, we pass 2 numbers, 5(now 6) and 10. obj is assigned the value of 10 but that obj still points to the same object in the method call, I would have thought, but @Henry Wong, you said that effectively it's a copy of the reference that it is passed. But if that is the case, why in @Ganesh Patekar's example the value changes and the object from Sachin then gets Virat?
     
    Dave Tolls
    Ranch Hand
    Posts: 2091
    15
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Beyond the "pass by value" stuff going on, there is a lot of auto-boxing and unboxing happening.

    The object created by the new Integer(5) call is passed into the method.
    On line 3 that reference is lost as the obj variable is reassigned to the autoboxed reference of var (10 at this point).
    That reference is then lost on the following line when obj is incremented, since it is unboxed, incremented and then reboxed into a new Integer.
    That reference is then lost as the method executes.

    Finally, the original reference is lost on line 9 when the testInts method has finished as val1 is incremented, so another round of unboxing etc happens.

    So I count 4 Integer objects created in that code, only one of which is still referenced at the end.
     
    Jason Attin
    Ranch Hand
    Posts: 232
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks, I think it is a little clearer. So a couple of points:
    -The reference is lost because the object has another reference assigned to it. In order to do that, you can either have a reference explicitly assigned to it (like obj = var++; ot rven obj++)
    -In Ganesh Patekar's code the reference is never lost, that's why the value of the object changes, presumably

    The + + were just a typo, sorry copying and pasting and since I couldn't edit my own post they're still there
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry I forgot to remove method testInts(...) from my previous code, please ignore that method.

    Jason Attin wrote:but @Henry Wong, you said that effectively it's a copy of the reference that it is passed. But if that is the case, why in @Ganesh Patekar's example the value changes and the object from Sachin then gets Virat?
    Because obj1 and p1 both have same reference, both are referring to the same object which is mutable and assigned new String value by accessing instance variable of that object so this effect will be seen anywhere we print the value of field name of that object.

    But in case of Integer, increment operation creates new object because Integer is immutable like String and that reference variable gets assigned by reference of this newly created Integer object having incremented value but reference variable of Integer which is in main method will keep referring same Integer object having value 5 until increment happens.

    Yes I completely agreed with Dave's explanation, 4 Integer objects get created.
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Jason I didn't notice your recent post, anyway 
     
    Jason Attin
    Ranch Hand
    Posts: 232
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks. OK so if we had a similar application, like say with strings like this:


    since String is also immutable as you've reminded me of, then the same principle is applied and even after the method has returned it will still print Jim and not Mike
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think you meant to say it will still print John and not Mike

    Of course It should print John.
     
    Liutauras Vilda
    Bartender
    Pie
    Posts: 2766
    111
    BSD VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Since it is OCA, need to be very careful. Not sure what kind of possible answers are provided, but it would print: John Jim
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic