GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Object References in JAVA. (Coming from C++) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object References in JAVA. (Coming from C++)" Watch "Object References in JAVA. (Coming from C++)" New topic
Author

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

David Moose
Greenhorn

Joined: Jun 11, 2004
Posts: 4
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.
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
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.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

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 ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Lucas Mac
Greenhorn

Joined: Jun 12, 2004
Posts: 10
junilu, i don't quite understand how the second example works out the way it does...can you explain a bit further?


yup
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Lucas, is there a particular part of Junilu's second example that you're unclear about? Did some part of the result suprise you?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Lucas Mac
Greenhorn

Joined: Jun 12, 2004
Posts: 10
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 ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.


42
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Jun 11, 2004
Posts: 4
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
 
GeeCON Prague 2014
 
subject: Object References in JAVA. (Coming from C++)