Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about passed by value

 
frank yang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is the result?
A. Compilation will fail.
B. The program prints �0�.
C. The program prints �3�.
D. Compilation will succeed but an exception will be thrown at line 3.
I know the Primitives are passed by value and Objects (references) are passed by reference.
The object reference itself is passed by value. So, it can�t be changed. But, the object can be changed via the reference.
In this case the method add3(i) passed the reference But why the result still can't be changed.
Who can explian this problem?
Thank u very much.
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by frank yang:

What is the result?
A. Compilation will fail.
B. The program prints �0�.
C. The program prints �3�.
D. Compilation will succeed but an exception will be thrown at line 3.
I know the Primitives are passed by value and Objects (references) are passed by reference.
The object reference itself is passed by value. So, it can�t be changed. But, the object can be changed via the reference.
In this case the method add3(i) passed the reference But why the result still can't be changed.
Who can explian this problem?
Thank u very much.

Frank,
It might sound wrong to you but in java,
everything is passed by value
when you are passing primitives, i guess you already know that they are passed as primitives.
When you are passing the references, you are passing the reference variable which holds a primitive value( that is the address of the object). The address of the object is nevertheless a primitive type and a copy of address is sent.
So basically you have two reference variables pointing to the same Object on the heap. In the method add3 , you are trying to change the copy of the reference value to point to some other object whereas the original reference variable i in the main method still points to the same object.
a couple of things
when u use the copy of reference variable to call some method on the object then the object will change.
but, here i = new Integer(3) is not changing the object but pointing to a new object
and one more important thing
Strings and primitive Wrapper Classes are immutable. This is a very important concept on the exam
Hope this helps
Sri
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank -
Let's keep track of reference variables and objects as we go thru the code...
in main() the line: Integer i = new Integer(0); creates 1 ref var of type Integer, and 1 Integer object, and refers to ref var to the object.
Next we pass A COPY of i into the add3() method. I wish we didn't call it 'i' again - because now we have 2 reference variables both named 'i'.
(I think this is a tricky thing the test makers try to do - they're testing two things at once, 1 do you understand passing stuff, 2 - do you know about 'hiding')
So now when we are in the add3() method, we still have 1 Integer object, but we have 2 ref vars, that both refer to that object! (The first is an instance variable, the second is a local variable.)
When the JVM runs the line: i = new Integer(val); We are creating a second Integer object, and referring it to the second ref var (unfortunately named i , jeez )
So at this point the first i refers to the first Integer object (with a value of 0), and

The second i refers to the new Integer object (with a value of 3).
Finally, the S.O.P. prints the value of the first Integer object.
I think this would be easier to understand if you renamed ALL of the occurences of i in the add3 method to something else, maybe i2.
They really are different reference variables.
 
frank yang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u for ur helps
Best regards,
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To avoid this type of thing, people sometimes try to avoid assigning to parameters.
This can be further enforced by making the parameter final.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should never change the contents of Objects that are passed into a method. It is much better to return a new Object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic