• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

objects r passed by references

 
sushant prabhu
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Siva Prasad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushant
in java...
Object references are passed by value
not by reference.
cheers
Siva
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic