This is the 34th original article by Why Technologies


This week is also a week of working from home. The picture above is my workstation at home. Compared with the first picture in last week’s article “What You Don’t Know about Dubbo Cluster”, the monitor support arm has been upgraded, and can be lengthened if it is short. Using a mechanical keyboard, let your fingertips enjoy that little bit of wonderful feedback from the red axis……

Still the same saying: to do a good job, you must sharpen your tools. Work from home. I mean it.

The two books below the display are the 2nd and 3rd editions of Understanding the Java Virtual Machine. Which is the main character of this article.

Do you have the second edition handy?

Here, turn to page 57. There is a “pit” in it. See if you found it and made notes on this page?


That’s okay. I’ll take you through this page and show you the notes I made when I first read the book three years ago.

What does page 57 of page 2 say?

Perhaps you haven’t read the book “Understanding the Java Virtual Machine in Depth (2nd edition).” But you must have seen the sample code on page 57 of this book:


Since the JDK 6 constant pool is in the method area, and after JDK 7 it is in the heap, running the above code with both versions of the JDK is a magical thing. Even when you run with JDK 8, you get unexpected results. Just listen to me slowly.

Let’s start with what intern does.

This method loads the first encountered string into the constant pool.

Look again at intern’s note:


For any two strings s and t, s.intern() == T.intern () is true if and only if s.squares (t) is true.

Let’s go back to the original code. The description quoted in the third edition is as follows:

This code, when run in JDK 6, will get two false, and when run in JDK 7, will get one true and one false.

The reasons for the difference are: In JDK 6, the intern() method copies the first encountered string instance into the string constant pool of the persistent generation and returns a reference to the string instance in the persistent generation. String instances created by StringBuilder are on the Java heap, so they must not be the same reference and will return false.

The intern() implementation of JDK 1.7 (and some other virtual machines, such as JRocki) no longer needs to copy instances of strings to the permanent generation. Since the string constant pool has been moved to the Java heap, it only needs to record the first instance reference in the constant pool. So the reference returned by intern() is the same as the string instance created by StringBuilder.

Returns false for str2 comparisons because the string “Java” already exists before executing stringBuilder.toString () and references to it already exist in the string constant pool, which does not comply with intern(). The string “computer software” appears for the first time, so returns true.


Dig a pit not to fill, pit cry readers

Do you have any doubts reading this? Do you feel something wrong?

Let’s look at the original:


Why is FASle returned in JDK 7? The red boxes above are key answers:

Because the string “Java” appears before stringBuilder.toString () is executed.

This phrase is “pit”, has appeared? Tell me where it came from! I was thinking the same thing as big Brother:


I first read this book in 2016. When I looked at this place, I was puzzled. It had been there before.

I don’t know where I found the words “Java is a keyword, already in the constant pool” in some specious blog. Also is serious classics copy up, although it is wrong description, although the word is a bit ugly……


Did you wonder then or when you watch it now?

I went through the book a few more times, and I remember clearly that the next time I saw it I thought the description “Java is the keyword, already in the constant pool” was not true ** so I crossed it and went back to the source again and found sun.misc. Version, Finally solved the question “where did it come from?”

Footnote filling for 3rd edition

The hole dug in 2013 (the year the second edition was published) was filled in on Zhihu by R University on October 1, 2016. This answer was also written by author Zhou Zhiming in the footnote of “Deep Understanding Java Virtual Machine (3rd Edition)” published at the end of 2019:


Inside RednaxelaFX is R big, a virtual machine to play to the extreme, with his own power to support the knowledge of Java half the sky man, I will be introduced in detail later.

All you need to know is that his answer is authority.


In this zhihu answer from R University, I help Zhou Zhiming fill in the hole greatly. I strongly suggest that you must check it out. The link is as follows:

https://www.zhihu.com/question/51102308/answer/124441115

Big R helped fill the hole

Here I just combine R’s answer and a little personal experience to talk about my own cognition.

When I read the book in 2016, I had just graduated from college and had just come out of the novice village. At that time, I had absolutely no idea about this problem, so I had to query the answer directly on the Internet.

Now, with a bit of experience, when I encountered this problem again, I would have thought of it even if I hadn’t given the big answer described in the book: in the example in the book, the second output false indicates that the “Java” string must have been in the string constant pool before the main method was called.

So how do we verify that?

Create a breakpoint on the first line of the main method. Debug will see Memory and filter out the String as follows:


Then double-click the java.lang.String filter to see the following image:


On this page we can continue filtering:


Sure enough, “Java” appeared before the program had even executed line 14.

From this result we can infer that the Java standard library may have classes that reference “Java” string literals that are added to the string constant pool when they are first referenced.

** And which class caused the “Java” string to be intern? **R mainly answers this question.

Let me intercept the final answer of R University and see his answer in the process of specific exploration, which is very hardcore:


We can see that the launcher_name field in sun.misc.Version is “Java” :


Java.lang.System: java.lang.System: java.lang.


According to the comments on the System class, it is automatically called by the virtual machine. Its initializeSystemClass method calls the sun.misc.version.init () method.

That’s where the truth comes out.

The Java standard library calls the init() method of Sun.misc.version during JVM startup. So Sun.misc.Version does class loading, and the actual assignment of static constant fields is done during the initialization phase of class loading, but since sun.misc.Version launcher_NAME is final, So the string “Java” referenced in the preparation phase was intern into the string constant pool.

Mentally review the five stages of class loading: load, validate, prepare, parse, and initialize.

In addition, the example code given in the book also has some limitations, R big is like this:

It's actually quite simple: First, this behavior is necessarily specific to a particular JDK/JRE implementation, because neither the Java language specification /JVM specification /Java SE library JavaDoc(which is part of the Java SE platform specification) nor does it mandate that any class must reference the string constant "Java." And it has to be the first class that makes "Java" intern - that's too boring to specify.

For example, in this example I run jdk8u212-b03, which is two true:


In this Version, the launcher_name of sun.misc.Version becomes “openJDK” :


So, according to our previous guess, the program like the following, the effect is the same:


Now you can see why the openJDK returns false.

Know what it is and why.


Love and Hate between R University and Zhou Zhiming

Who is R big?


I’ll start with an image on the back of Understanding the Java Virtual Machine (2nd Edition), which Rube wrote as a recommendation:


RednaxelaFx, engineer of Oracle HotSpot VM compiler team. (He is no longer with Oracle. Former Oracle JVM core developer, former Azul core developer, and now working for Databricks.

Look again at his zhihu homepage: https://www.zhihu.com/people/rednaxelafx/answers


If you go to Zhihu and just search RednaxelaFX (or even just use a search engine), you can get a lot of results. I just took a random snippet.

What are the top level Chinese programmers? Under this topic, one of the answers is just @ R big ID, without saying a word more, it has received 258 likes, the comments are also full of praise language, dry things, is his characteristic:


He and Zhou Zhiming, author of Deep Understanding the Java Virtual Machine, have had many in-depth exchanges on ItEye between 2010 and 2011, such as the following joke:


For example, the following joke:


Play to play, make to make, Zhou Zhiming also bluntly read R many articles, benefited a lot:


And in the acknowledgments section of the book, he specially thanks R University:


Having said all this, the point I want to make is actually one:

R big is a treasure, he is willing to share and communicate, with his own efforts to promote the study and research of JVM in China, if you want to understand the virtual machine, compilation principle and programming language related knowledge, he is a person you can not get around. He deserves to be known by more programmers.

If you didn’t know him before, but now you know him after reading my article, I’ve done my job.

He is a “code” zong strong, terrible, but from his various answers, blog posts, you can feel humble, meticulous, systematic, patient, professional, rigorous….. As one comment put it:

In today’s increasingly fickle tech world, it feels like he is what the chairman said he was: a pure man, a moral man, a man above vulgar interests, a man who is good for the people.

We do programming, to learn from him, to pay tribute to him.


Then attach a large R data collection of links, is full of treasures for you to discover: https://zhuanlan.zhihu.com/p/25042028

One last word

If you find something wrong, please leave a message and point it out to me so that I can modify it.

If you think the article is good, your retweets, shares, likes and comments are the biggest encouragement to me.

Thank you for reading, I insist on original, very welcome and thank you for your attention.

The above.

Welcome to pay attention to the public account [WHY Technology], adhere to the output of original. Share technology, taste life, wish you and I progress together.