1. The difference between String, StringBuilder and StringBuffer
1. String is a string constant, StringBuilder and StringBuffer are string variables
After the String object is created, if it is changed, it will recreate a String object, and let the reference variable point to its reference address again, and both StringBuilder and StringBuffer are variable;
2. StringBuilder is thread-unsafe, StringBuffer is thread-safe
StringBuffer-related methods add the thread synchronization keyword synchronize keyword, so it is thread-safe, but the efficiency is lower than StringBuilder.
2. Why is String designed to be immutable?
1. The optimization needs of the string constant pool
The string constant pool is an optimization strategy for String strings. Before creating a string object, first check whether the string constant already has the string (obj1.equal(obj2)), and return the string directly in the string constant Pool reference, if String is mutable, this optimization strategy is invalid;
2. Allow the String object to cache the HashCode.
The invariance of the string ensures the uniqueness of the hashcode. The immutable hashcode can be cached without recalculation, which improves the efficiency of the hashmap that uses String as the key value. This also reflects why hashmap Most of the reasons for using String as the key value;
3. Multi-threaded use safety
Strings are immutable, so multiple threads can share a string instance without additional thread synchronization;
4. The class loader needs
The class loader uses strings, and immutability provides security so that the class can be loaded correctly; for example, when loading the java.sql.Connection class, if the value is changed to myhacked.Connection, it will cause unknown damage to the database;
5. Security
If the string is immutable, it will cause serious security problems; for example, the user name and password of the database are passed in the form of strings to obtain the connection to the database, and in socket programming, the host name, etc. are passed in the form of strings. If the string is mutable, hackers can easily change the value of the string object, causing security holes.
3. The difference between String directly creating objects (String s="abc") and the intern() method
When creating a string object, the two first go to the string constant pool to find it. If there is, the reference of the string is returned directly. If not, the string constant pool is created and returned. What is the meaning of
Test codeTest.java
String s1 = "ab";
String s2 = "c";
String s3 = "abc";
System.out.println(s3=="ab"+"c"); //true
System.out.println(s3==s1+s2); //false
System.out.println(s3==(s1+s2).intern()); //true
Compile the code Test.class
String s1 = "ab";
String s2 = "c";
String s3 = "abc";
System.out.println(s3 == "abc");
System.out.println(s3 == s1 + s2);
System.out.println(s3 == (s1 + s2).intern());
"ab"+"c"; string splicing can determine its value at compile time, and then can determine whether the string exists in the string constant pool at compile time; but s1+s2; string reference splicing needs to be done at runtime After getting the result, you can’t count on the compiler’s string constant pool optimization strategy. At this time, the role of the intern method is reflected. It is determined at runtime whether there are string objects that need to be created in the constant pool. If so, return its string constant pool references. Therefore, the conclusion drawn: String direct assignment and intern method in the optimization strategy of the string constant pool, one is reflected in the compilation period, and the other is in the runtime.
4. What is the difference between StringBuilder and "+" sign?
1. Splicing string constants
test class
String s1 = "a" + "b" + "c";
String s2 = new StringBuilder().append("a").append("b").append("c").toString();
compile class
String s1 = "abc";
String s2 = "a" + "b" + "c";
ByteCode
L0
LINENUMBER 6 L0
LDC "abc"
ASTORE 1
L1
LINENUMBER 7 L1
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.<init> ()V
LDC "a"
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
LDC "b"
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
LDC "c"
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
ASTORE 2
The concatenation of string constants using "+" has been completed at compile time, while the concatenation of strings using StringBuilder needs to be completed at runtime. Therefore, the efficiency of simple string constant splicing "+" should be higher than that of StringBuilder
2. Concatenate strings and quotes
test class
String s1 = "a";
String s2 = new StringBuilder().append(s1).append("b").append("c").toString();
String s3 = s1 + "b" + "c";
compile class
String s1 = "a";
String s2 = s1 + "b" + "c";
String s3 = s1 + "bc";
``
字节码ByteCode
```c
L0
LINENUMBER 6 L0
LDC "a"
ASTORE 1
L1
LINENUMBER 7 L1
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.<init> ()V
ALOAD 1
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
LDC "b"
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
LDC "c"
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
ASTORE 2
L2
LINENUMBER 8 L2
NEW java/lang/StringBuilder
DUP
INVOKESPECIAL java/lang/StringBuilder.<init> ()V
ALOAD 1
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
LDC "bc"
INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString ()Ljava/lang/String;
ASTORE 3
It can be seen from the bytecode that the "+" concatenated string refers to the underlying layer or uses StringBuilder
reference link
What is the difference between String, StringBuilder and StringBuffer in Java?
Why is the String class immutable in java?
What is the difference between intern and String a="abc" in java String?