About a class built into the Java JDK: java.lang.string

1. String indicates the String type and belongs to the reference type

2. In Java, String objects are enclosed in double quotes. For example: “ABC “,” Hello world”

3. In Java, a string object enclosed in double quotes is immutable once created

Why is String immutable? The source code for the String class has an array of bytes [] decorated with final. Because once you create an array length is immutable, and is the final modification once a reference to an object, not to point to other objects, so the String immutable "ABC" why not become a "abcd" StringBuffer/StringBuilder variable? StringBuffer/StringBuilder is actually an internal is not final modification byte [] array, StringBuffer/StringBuilder initialization capacity is 16, when after full expansion, The underlying call array copy method (System. Arraycopy ()) to increase, so the StringBuffer/StringBuilder is suitable for the frequent concatenation operation is suitable for the string.Copy the code

In the JDK, strings enclosed in double quotes are stored in the string constant pool in the “method area”

Public class StringTest01 {public static void main(String[] args) {// Create 3 String object String s1 = "abcdef"; String s2 = "abcdef" + "xy"; S3 = new String("xy"); s3 = new String("xy"); }}Copy the code