aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question about passed by value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A question about passed by value" Watch "A question about passed by value" New topic
Author

A question about passed by value

frank yang
Greenhorn

Joined: Feb 19, 2002
Posts: 22

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.


Look at me and hear me<br />Because I am here<br />Just for that
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
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

Joined: Oct 14, 2002
Posts: 8764
    
    5
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.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
frank yang
Greenhorn

Joined: Feb 19, 2002
Posts: 22
Thank u for ur helps
Best regards,
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
To avoid this type of thing, people sometimes try to avoid assigning to parameters.
This can be further enforced by making the parameter final.


comp.lang.javascript FAQ: http://jibbering.com/faq/
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You should never change the contents of Objects that are passed into a method. It is much better to return a new Object.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question about passed by value
 
Similar Threads
method..
about pass by value
please explain what happens in this code?
o/p needed
WRAP class question and nested loop question!!