File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Need proof for  Modifying a String Object will result in creation of a new Object under Heap Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Need proof for  Modifying a String Object will result in creation of a new Object under Heap " Watch "Need proof for  Modifying a String Object will result in creation of a new Object under Heap " New topic
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
    
    3

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
    
    4
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Need proof for Modifying a String Object will result in creation of a new Object under Heap
 
Similar Threads
string equal(); question
String
String, String Buffer and String Builder
How many objects created?
Why does discarding String pool objects waste memory?