Discussion on the type of Sting

  string type and basic data types are often proficient with us, but it is not classified as basic data types, he is a special reference type.

  We look at the interpretation of his jdk:

 

   String is java.lang.String bag in front with a final modification, modification and therefore can not inherit this class. In order to improve efficiency and save space, we typically do referenced mosaic StringBuffer class string. For example, String s = "hello"; s = s + "world!"; After two lines of code execution contents of the original String object is not changed in the end?

  Because String is designed to be immutable (the immutable) class, so that all objects are immutable. s original point to a String object, content is "hello", and then we s were s + operator, s pointing to another String object, content that is not "hello world!", the original of that object also exists in memory, just the reference variable s not point to it. If you often String string of various modifications

That it is bound to cause a lot of memory overhead. At this time we should use StringBuffer class, and it allows modification, but not every different string must generate a new object. Moreover, the two types of object conversion is also very simple.

Guess you like

Origin www.cnblogs.com/roc-cy/p/12077612.html