• 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

Object References in JAVA. (Coming from C++)

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I come from a C++ world and now moving to JAVA.
Consider piece of Code.
void foo(String str)
{
str = "Hello" + str;
}

When one calls foo, the string passed remains unchanged.
Similar thing in C++ (passing object my reference) modifies the object.

Can you explain to me in detail what is happening internally here?
I understand that if I call a method on the object it could get modified (in this case since String in immutable that does not apply).

Does Java copy the reference on the stack? how is it different than in C++ world?

thanks a bunch.
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding is that it works almost exactly like C++ in the fact that java passes method arguments by-reference (and since all Java String objects are 100% immutable, the String wouldn't change anyway, you'd just get a reference pointing to a new String). I also believe that the exact implementation of how the stack/heap/etc works varies from platform to platform, though I could be wrong about that.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All parameters in Java are passed by value. This means that the value of the actual parameter can never be changed as a result of calling a method.

The catch is if you pass an object reference. The actual parameter is the reference, not the object. That means that while you cannot change the reference and make it refer to another object, you can still change the object being referenced. Clear as mud?



Notice that even though in foo(), str is made to reference a new string, the actual parameter bar in test() does not change. To illustrate further with mutable objects, let's look at some code that passes around arrays, since arrays behave like objects in Java.



HTH

[JL: corrected for loop; was missing increment]
[ June 12, 2004: Message edited by: Junilu Lacar ]
[ June 14, 2004: Message edited by: Junilu Lacar ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
junilu, i don't quite understand how the second example works out the way it does...can you explain a bit further?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lucas, is there a particular part of Junilu's second example that you're unclear about? Did some part of the result suprise you?
 
Lucas Mac
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well dirk, i understand the result: b is {1, 2, 3, 4}...however, i don't understand why a is changed when in the first example, bar was not modified..

edit:
actually, i think i see now. in the first example: 'str = "Hello " + str;' made str reference a new string...but in the second example the actual elements in the array were accessed/modified.
[ June 13, 2004: Message edited by: Lucas ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you come from C I'll try to explain it in a C context.

In Java all parameters are passed by value. In C that would mean you're always setting const parameters to functions.
The crux is that unlike in C where when you pass a struct you pass the actual struct itself in Java you instead pass a pointer to that struct.
The pointer cannot change, the data that that pointer refers to can.

For example,

has no effect because you attempt to change the pointer (reference).

does take effect as it changes the content of the object referred to and not the value of the reference (pointer).

In Junilu's first example, a new value is assigned to the reference (due to the vagaries of Java String handling, the example may have been a little obscure), in the second the content of the data referred to by the reference is changed.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lucas,

You changed your display name to one that doesn't comply with our naming policy. Please change your display name to comply.

Thanks Pardner! Hope to see you 'round the Ranch!
 
David Moose
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you your replies.
I fully understand how this works.
Now will have to work on registering this fact whenver I read JAVA source code
 
reply
    Bookmark Topic Watch Topic
  • New Topic