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
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).]