my dog learned polymorphism*
The moose likes Beginning Java and the fly likes objects r passed by references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "objects r passed by references" Watch "objects r passed by references" New topic
Author

objects r passed by references

sushant prabhu
Ranch Hand

Joined: Mar 22, 2001
Posts: 66
in java objects r passed by references i.e the memory address of the variable which points to the memory address where the object is stored . Now suppose i pass a String object as a parameter to a method an i change its value inside that method i cannot see the changed value once i try to display the value out of the method block.

whereas if i pass any other object to a method and change any of its variable values using the object as a handle it reflects the
new value even after the method block expires.
please can somebody ellaborate.
i know in java strings r immutable read only objects . but if i have a object which has string as on of its variable ( int as in the above case) and i change the value of the string using the object as a handle the new value of the string which is an instance variable is also reflected outside method blocks


Sushant Prabhu<br />SCJP2<br />SCWCD<br />OCA9i
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
Sushant
in java...
Object references are passed by value
not by reference.
cheers
Siva
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Not pretty, but it serves the purpose:
What is happening, inside the methods, is that there is a new String object created with the original text from 's', plus the additional text appended. s then gets pointed at this new String object. But that is only s, the method-scoped parameter variable. Not the String input, outside the method (it still points to the original, unchanged, immutable string.

Basically, you must return the object if ever you make a 'new' one (either explicitly or by using = or += with Strings)

I can already taste another "pass by value" / "pass by reference" holy war brewing.


[This message has been edited by Mike Curwen (edited April 04, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Java does all passing by value. It just happens to be that the value it passes for objects is the value of the pointer to that object.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Now suppose i pass a String object as a parameter to a method an i change its value inside that method i cannot see the changed value once i try to display the value out of the method block.
This is correct because when you do something like:


String s = "A String";
s = "A Different String";


you are creating a brand new String object with a completely different address in memory. Remember:


s = "A Different String"; is the same thing as
s = new String("A Different String");
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: objects r passed by references
 
Similar Threads
pass by reference
If Classes are passed by reference why doesnt this work ??
wrapper classes
Passed By Reference?
A question about passed by value