Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - The difference between string and stringbuffer and stringbuilder
The difference between string and stringbuffer and stringbuilder
The differences between string and stringbuffer and stringbuilder are: 1, mutable and immutable 2, initialization 3, string modification 4, whether the equals method is implemented 5, and whether the hashCode method is implemented.

1, mutable and immutable

String class is an immutable class, that is, after the String object is created, the strings in the object cannot be changed before the object is destroyed. StringBuffer and StringBuilder both inherit from the AbstractStringBuilder class. In the AbstractStringBuilder, character arrays are also used to store strings, which are variable classes.

2. Initialization method

When creating a String object, it can be initialized by using the constructor Stringstr = New String ("Java") or directly by using the assignment method String s = "Java ".

3. String modification method

The modification method of String string is to create a StringBuffer, then call the append method of StringBuffer, and finally call the toString () method of StringBuffer to return the result.

4. Is the 4.equals method implemented?

String implements the equals () method and hashCode () method, and the result of new string ("Java "). Equals (new string ("Java")) is true; ;

5. Has the 5.hashCode method been implemented?

StringBuffer does not implement the equals () method and the hashCode () method.