• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

A question about passed by value

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u for ur helps
Best regards,
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic