StringStringBufferImmutableMutableStrings=newString("karthik");StringBuffersb=newStringBuffer("karthik")s.concat("reddy"); sb.append("reddy");System.out.println(s);System.out.println(sb);O/P:karthik O/P:karthikreddy--->once we created aStringobject ---->once we created aStringBufferobjectwe can't perform any changes in the existing we can perform any changes in the existingobject.Ifwe are trying to perform any object.Itis nothing but mutablity ofchanges with those changes anewobject of aStrongBufferobjectwill be created.Itis nothing butImmutabilityof aStringobjectUseString--->Ifyou require immutabiltyUseStringBuffer---->Ifyou require mutable + threadsafetyUseStringBuilder--->Ifyou require mutable + with out threadsafetyStrings=newString("karthik");--->here2objects will be created one is heap and the other is in stringconstantpool(scp) and s is always pointing to heap objectStrings="karthik";--->Inthiscaseonly one object will be created in scp and s is always pointing to that object only
Tuesday, 29 October 2013
difference between string and stringbuffer
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment