| Author |
Need proof for Modifying a String Object will result in creation of a new Object under Heap
|
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
Hi ,
How can i prove that , Modifying a String Object will result in creation of a new Object under Heap . Please suggest , Thanks .
|
Save India From Corruption - Anna Hazare.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
String objects are immutable - there is no way to modify a String object. The way you phrased the question is incorrect.
All methods in class String that do something with the content of the string, do not modify the original string - instead, they return a new String object that contains the modified content of the original string.
To prove this, look at the API documentation of class java.lang.String or look at the source code (which you can find in the file src.zip in your JDK installation directory), you will find that there are no methods in the class that modify the content of a String object.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Ravi Kiran Va
Ranch Hand
Joined: Apr 18, 2009
Posts: 2234
|
|
Thanks for the answer .
Yes you were right the question itself is wrong .
I actually wanted to to print the memory Locations of a String Object being created using hashcode ( i was not successfull , as i could able to print only the reference of the String location which is always the same :cool
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
|
If you go through the documentation for String#hashCode, Object#hashCode() and System#identityHashCode(), you will find whether it is possible to find the memory location or not.
|
 |
 |
|
|
subject: Need proof for Modifying a String Object will result in creation of a new Object under Heap
|
|
|