Last time we talked about arrays in Java, which was actually a preparation for this chapter. The String class is one of the most commonly used classes in Java code. Today we will talk about it.

We declare a char[] array, value, and a hash variable of type int (we’ll talk about hash later).

Let’s click on the constructor to see:

Years ago, WHEN I saw this code, I was confused. Yes, I was now preparing to construct a String object. Where did the original object come from? When was it constructed?

In Java, when a value is enclosed by double quotes (as in this example, “ABC “), the JVM checks to see if ABC is in the constant pool. If not, it initializes ABC as an object and puts it in the constant pool. String STR = new String(” ABC “)

The new keyword is then processed to make space in the heap memory, and since the hash field is an int, the initialized member variable defaults to 0.

To handle constructor logic, hash is a value type that assigns values directly, and array is a reference type that points directly to the address.

Continue to above

String str2 = new String(” ABC “)

Use IDE debug to see that the char array already contains ‘a’,’b’, and ‘c’ values.

Let’s take a look at how the String API is implemented:






Very simple right, terrible is not the source code is difficult to read, but do not want to, afraid to read the heart of the source code. If the article gets your approval, please give my article a thumbs up, your approval is my motivation to continue.

Note: The figure in this article is just a sketch of what the object looks like in the JVM for your convenience. If you want to explore the JVM in more depth, I recommend reading understanding the Java Virtual Machine in Depth.


If you liked this series, please give me a thumbs up or share it with me. Your support is what keeps me going, and leave a comment in the comments section where you can find out what you want to know about this column.

Arrays in Java – Java stuff

Next: Java primitive data types and reference types – Java stuff


Reprint unlimited welcome, but please indicate “author” and “original address”. Please keep this paragraph in the article, thank you for your respect for the author’s copyright. For commercial reprint or publication, please contact the author for authorization.