public static void main(String[] args) {
    String str1 = new StringBuilder("Computer").append("Software").toString();
    System.out.println(str1.intern() == str1);
    String str2 = new StringBuilder("ja").append("va").toString();
    System.out.println(str2.intern() == str2);
}
Copy the code

Understanding the Java Virtual Machine: Advanced JVM Features in Action (version 3) Why would they do that?

The author wants to rely on Sue me, once do not believe, twice you should believe it! Witty as I am. To prove how resourceful I am, I took a stab at it.

Well, I guess I read it wrong, a million times… My idea must be a problem, let colleagues run to try!

Why is that? Why is that? Why is that? You’re leaving me! (Sorry about that). Why true and flase.

String constant pool jdk1.6 and jdk1.7 and above will be stored on the Java heap. Jdk1.6’s intern() method makes a copy of the string to a permanent generation when first encountered, whereas JDk1.7 and above just record the first occurrence of instance references in the constant pool. The constant pool records the address at which the string first appears. This object will never be collected by the garbage collector. Of course I’m afraid to ask…

Speaking of which, let’s look at the intern() method! (jdk1.8)

/**
* Returns a canonical representation for the string object.
* <p>
* A pool of strings, initially empty, is maintained privately by the
* class {@code String}.
* <p>
* When the intern method is invoked, if the pool already contains a
* string equal to this {@code String} object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise, this {@code String} object is added to the
* pool and a reference to this {@code String} object is returned.
* <p>
* It follows that for any two strings {@code s} and {@code t},
* {@code s.intern() == t.intern()} is {@code true}
* if and only if {@code s.equals(t)} is {@codetrue}. * <p> * All literal strings and string-valued constant expressions are * interned. String literals are defined in Section 3.10.5 of the * <cite> the Java&trade; Language Specification</cite>. * *@return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/
public native String intern(a);
Copy the code

Forgive me if I don’t understand, so Google translated it. When you call an intern method that already contains a string equal to this object, you retrieve the string from the pool and return it. Otherwise, the object address is added to the pool and a reference to the object is returned. Well, same meaning as above.

I know the first one is true. I’m asking you why the second one is false. It’s bullshit.

Don’t get mad, man. I’ll tell you all about it. Actually, I don’t know. Then I read an article on Zhihu entitled how to Understand the String.Intern () method in the second edition of Understanding the Java Virtual Machine. The Java string was already in the constant pool during initialization.

Is that bad luck?

Take an example that makes me doubt life. Ah…

String s = new String(“s”) proves that he created two objects.

    public static void main(String[] args) {
    String s = new String("s");
    System.out.println(s.intern() == s);
}
Copy the code

The result is false, so we created two objects. The first is new String(“s”), and the second is s for the constant pool. Hahaha.

How do you prove that Java starts with no S in the constant pool? After all, Java has a constant pool in the first place.

Er…

String s = new String(” Java “) creates several objects. Hahaha, it creates an object. No one is arguing with me this time. Hahaha ~