java - How can I get old String object value once current object modified -
if string immutable object , whenever modify string object create instance i.e. in memory have 2 objects currently. how can value of first initialized value?
public class mutablevsimmutable { public static void main(string[] args) { string str1 = new string("dilip"); system.out.println(str1.hashcode()); str1 = str1 + " singh"; system.out.println(str1.hashcode()); **// here want first initialized value of "str1" i.e. "dilip"** } }
string immutable in java when manipulate string,a new object created , reference previous object lost.there no way recover it.java provides stringbuffer , stringbuilder deal kind of situation.important thing note no new object created when manipulate stringbuilder object.you can use
stringbuilder str1 = new stringbuilder("dilip"); system.out.println(str1.hashcode()); str1 = str1 + " singh"; system.out.println(str1.hashcode());
Comments
Post a Comment