• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need proof for Modifying a String Object will result in creation of a new Object under Heap  RSS feed

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

How can i prove that , Modifying a String Object will result in creation of a new Object under Heap . Please suggest , Thanks .
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!