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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -