This is the 10th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

How many objects does each of the following code build

Problem a

String zy1 = "Xiao zhu";
String zy2 = "Xiao zhu";
Copy the code

Question 2

String zy1 = "Xiao zhu";
String zy2 = "Zhu";
Copy the code

Question 3

String zy1 = new String("Xiao zhu");
String zy2 = new String("Xiao zhu");
Copy the code

Problem four

String zy1 = new String("Xiao zhu");
String zy2 = new String("Zhu");
Copy the code

Problem five

String z = "Small";
String y = "Zhu";
String zy = z + y;
Copy the code

Question 6

String zy = "Small" + "Zhu";
Copy the code

parsing

Is there anyone who doesn’t even know question one, “This is a constant, this must not have created an object”? In the JAVA Virtual Machine (JVM), there is a String pool in which many String objects are held and can be shared, so it improves efficiency. Since the String class is final and its value cannot be changed once it is created, we don’t have to worry about the clutter of sharing strings. The String pool is maintained by the String class and can be accessed by calling the intern() method. Mumbling a lot of what I didn’t understand a word, ha ha. String zy = new String(new char[]{‘ x ‘,’ x ‘}); String zy = new String(new char[]{‘ x ‘,’ x ‘}); Why did I do that? Look at the String source code.

/** The value is used for character storage. */
private final char value[];

/** Cache the hash code for the string */
private int hash; // Default to 0

public String(String original) {
    this.value = original.value;
    this.hash = original.hash;
}
Copy the code

And then let’s look at problem number one

String zy = new String(new char[] {'small'.'zhu'});
String zy1 = zy;
String zy2 = zy;
Copy the code

Do you see at once that only one object has been created? So let’s move on to question two

String z = new String(new char[] {'small'.'zhu'});
String y = new String(new char[] {'big'.'zhu'});
String zy1 = z;
String zy2 = y";
Copy the code

Two objects

Let’s move on to question number three

String zy = new String(new char[] {'small'.'zhu'});
String zy1 = new String(zy);
String zy2 = new String(zy);
Copy the code

Three objects

Let’s move on to number four

String z = new String(new char[] {'small'.'zhu'});
String y = new String(new char[] {'big'.'zhu'});
String zy1 = new String(z);
String zy2 = new String(y);
Copy the code

Four object

Let’s move on to question five

String z1 = new String(new char[] {'small'});
String y1 = new String(new char[] {'zhu'});
String zy1 = new String(new char[] {'small'.'zhu'});
String z = z1;
String y = y1;
String zy = zy1;
Copy the code

Now, this is interesting, because we built three objects, so why don’t we do strings.

Let’s move on to question six, which I don’t understand in my way, because they add up first. So it’s the same thing as String zy = “Xiao Zhu “; This creates an object.

conclusion

String is a lot of things, an article also can not finish, today a brief introduction to the next some like the interview asked content, is also recently seen an interesting thing, so I wrote this article, you see the officer can have their own understanding?