String-kun, why are you so beautiful

Talk about strings in java

foreword

tips

Take a look at this classic

String s1 = "a";
        String s2 = "b";
        String s3 = "ab";
        String s4 = "a" + "b";
        String s5 = s1 + s2;
        String intern = s4.intern();
        System.out.println(s3 == s4);
        System.out.println(s3 == s5);
        System.out.println(s3==intern);
        System.out.println(s5==intern);

Brothers who are interested in commenting on the answer, let’s communicate with each other,

Have you figured out the answer? After you have figured out the answer, look down and publish the answer.

Is it the same as the big guy’s comment? Let’s analyze it one by one

s3==s4?

Here we want to mention a structure in the java memory structure, called StringTable , its main function is to store objects such as string constants, the following code

String s="123"
String s2="123"

The s object here is stored in the string pool. For the s2 object, because the string stored in s2 is the same as s, there is no need to store new objects in the string pool, so s and s2 actually point to an object.

If you can understand the above, we will propose another feature. When compiling java, “a” + “b” and “ab” will be treated as the same constant, so when dealing with two pieces of code,

String s="ab"
String s2="a"+"b"

and

String s="ab"
String s2="ab"

The processing is the same, i.e. s is the same as the object pointed to by s2, so s3==s4

s3==s5?

Here we have to talk about the following string of codes. I never thought that such a simple string of codes would be so magical. Come and see it with the blogger.

String a="a";
        String b="b";
        String c=a+b;

First of all, let’s decompile and take a look.
Even, let’s take a look at this annoying code, but fortunately, it gave us a comment later.
The operations of 0-5 are very normal, that is, taking the string constants corresponding to #2 and #3 in the constant pool, which corresponds to the first two lines of code.
But 6 is a bit strange. There is new, which means that a newly created object can be queried through the constant table. You can also read the comments below. It can be seen that a StringBuilder object is created, and the intermediate command is skipped directly. Let’s see In the next 21, it is found that the toString method is called. Let’s take a look at this method.
We find that this method originally returns a newly created object, which is clear now. It turns out that when a+b is spliced, StringBuilder will be called to generate a new object. To assign to c, because the new object is to create a new object in the heap.

back to the above

String s3 = "ab";
        String s5 = s1 + s2;

s3 points to the corresponding “ab” string object in the string pool, and s5 points to another newly generated object in the heap, so s3!=s5

internal==s3?

This is relatively simple, we only need to know that the intern() of the string is to store the string that calls the method into the string pool, because the string pool is a hash table structure, so when storing it again, first judge Whether there is a string object with the same content in the string pool, if not, it will be stored, and if there is, no operation will be performed. In both cases, the corresponding string object in the string pool will eventually be returned, so the intern point is actually the string pool. The string object corresponding to “ab” in the middle, so s3=intern, for the same reason, from the previous part we can know that s5 is a newly generated object, so intern!=s5

Summarize

I have been supplementing my java knowledge for a while. This blog can say that I made a note after learning the online class. If the content is wrong, please correct me, thank you for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *