• 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

Why isnt this working the way i think....Pass by reference vs pass by value

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I know this might be a very basic question, but for some reason it isnt behaving they way i thought it should.



The output is :

i : 2
y : 1 (understand, pass by value)
iInt : 2
yInt : 1 (dont understand)
iString : Whaaaa
yString : grrr (dont understand)

Shouldnt the last to be the same as the object they got assigned to. Shouldnt they pass there value by reference, so that when the reference values change, they have the new value?

What am i not getting here.

Thanks
D
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is nothing to do with pass by reference/value. In this bit of code the first line declares a variable iInt that is an Integer reference and it sets it to reference an Integer object with the value 1.
The second line declares a second variable yInt that is also an Integer reference and sets it to point to the same object as iInt.
At this point there is one Integer object with two variables referencing it.
The third line then creates a new Integer object with the value 2 and sets iInt to reference it. You now have two Integer objects, one of which is referenced by iInt (the one with value 2) and one by yInt (the one with value 1).

The same explantion applies to the String objects.
 
Derick Potgieter
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i get that, but why doesnt this work.

Please can you give me an example of where pass by reference is working.

Integer iInt = new Integer(2);
Integer yInt = iInt;

iInt *= yInt;

System.out.println("iInt : " + iInt);
System.out.println("yInt : " + yInt);

String iString = "grrr";
String yString = iString;

iString += "Whaaaa";


System.out.println("iString : " + iString);
System.out.println("yString : " + yString);

thanks
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is NO SUCH THING as pass by reference in java. EVERYTHING is pass by value.

What you must understand is exactly WHAT is being passed.

when you say

Integer iInt = new Integer(2);

you are creating two things. you are creating the Integer object that has a value of 2, which lives on the heap. this can be thought of as a house you have built somewhere.

you also create a REFERENCE to the object, that you have called iInt. that points to the object. It's like having a slip of paper with your house's address on it.

when you say

Integer yInt = iInt;

you are saying "create a new reference and copy the value from the old reference onto it". In other words, you are copying your home address from the paper labeled "iInt" onto a paper labeled "yInt". You have two slips of paper, both of which have the same address on them.

now you do this:

iInt = new Integer(2);

you build a SECOND home. You write the address on the iInt piece of paper, erasing the old address. This in no way effects what is written on the other slip.

it's the same with Strings, or Maps, or any other object.
[ October 02, 2007: Message edited by: Fred Rosenberger ]
 
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object reference stores the location where the object is allocated at. It is the location reference that gets overwritten, but not the object itself.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic