Before writing, let’s take a look at a few questions. If you already know a lot about these questions, you don’t need to read this article. If not, you can read my thoughts.
Question 1:
public static void main(String[] args){
String t1 = new String("1");
t1.intern();
String t2 = "1";
System.out.println(t1 == t2);
String t3 = new String("2") + new String("2");
t3.intern();
String t4 = "22";
System.out.println(t3 == t4);
}
Copy the code
Answer output:
JDK1.6 is false false
JDK1.7 is false true;
Question 2(Transpose the statement in question 1)
public static void main(String[] args){
String t1 = new String("2");
String t2 = "2";
t1.intern();
System.out.println(t1 == t2);
String t3 = new String("2") + new String("2");
String t4 = "22";
t3.intern();
System.out.println(t3 == t4);
}
Copy the code
Answer output: false false
For these two questions, read a few people’s blogs, it can be said that a hundred flowers bloom, the more see more meng force
Question 3
public static void main(String[] args){
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
System.out.println(c == d);
System.out.Println(e == f);
System.out.println(c == (a + b));
System.out.println(c.equals(a+b));
System.out.println(g == (a + b));
System.out.println(g.equals(a + b));
}
Copy the code
Answer output:
true
false
true
true
true
false
Question 4:
What is the relationship between a runtime constant pool and a string constant pool? Contain?Copy the code
Before we solve the problem, let’s take a quick look at some constant pools (mostly from Zhiming Chou’s Inside Java Virtual Machine book).
Several constant pools in the JVM
1. Class file constant pool
The Constant Pool Table is used to store literal and symbolic references generated at compile time. The Constant Pool Table is used to store literal and symbolic references generated at compile time. The Constant Pool Table is used to store literal and symbolic references generated at compile time.
Here’s a quick explanation of literal and symbolic references
literal
Literals are similar to constants and include:
- Text String: This is the String we see in our code, for example String a = “aa”. Where “aa” is the literal.
- A variable modified by final.
Symbolic reference
It mainly includes the following constants:
- Class and interface and fully qualified name: For the String class, for example, the fully qualified name is Java /lang/String.
- Field names and descriptors: Fields are variables declared in a class or interface, including class-level variables (static) and instance-level variables.
- The name and descriptor of the method. A descriptor is equivalent to a method’s parameter type + return value type.
2. Run-time constant pool
The constant pool in the class file we talked about above goes into the runtime constant pool in the method area after the class is loaded. It is also important to note that the runtime constant pool is globally shared, with multiple classes sharing the same runtime constant pool. And the class file will only have one constant pool of the same string at run time.
Note that the runtime constant pool exists in the method area.
3. String constant pool
We know from the name that the string constant pool will be used to hold strings, which means that the text strings in the constant pool will go into the string constant pool when the class loads.
What is the relationship between string constant pools and runtime constant pools? Literals in the constant pool are added to the runtime constant pool when the class is loaded. Literals include text strings. It’s in the method area.
In JDK1.7, however, the string constant pool was moved out of the method area and into the heap.
So we can infer that in JDK1.7 and later, the runtime constant pool does not contain a string constant pool; the runtime constant pool resides in the method area, while the string constant pool resides in the heap.
Having said that, now let’s begin to address the questions raised above.
To solve the problem
Question 1:
public static void main(String[] args){
String t1 = new String("1");
t1.intern();
String t2 = "1";
System.out.println(t1 == t2);
String t3 = new String("2") + new String("2");
t3.intern();
String t4 = "22";
System.out.println(t3 == t4);
}
Copy the code
Answer output:
JDK1.6 is false false.
JDK1.7 is false true;
Before we get to that, let’s look at another common interview question.
String t = new String("tt");
Copy the code
If there is only one line of code in the program, how many objects does that line create?
We said that “tt” is a literal, so it will be in the String constant pool after the class is loaded, that is, before String t = new String(“tt”) is executed, the String constant pool will have created the String object “tt”, as we all know, The new keyword creates an object in the heap.
So, this code creates two objects. One in the heap, one in the string constant pool.
So how many objects does this code create?
String t1 = new String("tt");
String t2 = new String("tt");
Copy the code
A: This code creates three objects. As we said above, the string constant pool will only hold one copy of the same string. That is, before these two lines of code are executed, the string constant pool has created an object with the content “TT”. After these two lines of code execute, two more are created in the heap, so three are created.
So how many objects does this code create?
String t = "tt";
Copy the code
Answer is 1, before this code execution, have created a string constant pool “tt” objects, but as a result of this line of code is not used the new method, so the virtual opportunities in string constants in the pool to find whether there is content for “tt” string objects, if you have, the references of the string is returned directly, so the final result only creates an object.
Back to our problem, let’s explain the String intern method here.
For example, we called T.intern ().
In JDK1.6, after this method is called, the virtual machine looks in the string constant pool to see if there is an object with the same content as “tt”, returns the object if there is, and adds the object to the string constant pool if there is not. Notice that you are adding this object to the string constant pool.
After JDK1.7, if intern is called, the virtual machine looks in the string constant pool to see if there is an object with the same content as “tt”, and returns the object if not. The reference copy of this object is added to the string constant pool in the heap. Notice that you are adding a reference to the object in the heap.
Now start analyzing the code in question
T1 = new String(“1”).
The string constant pool already has the object “t” before this code executes, and after this code executes, it creates an object “T” in the heap, where T1 points to the object in the heap.
t1.intern();
After this code executes, it looks for an object with the content “t” in the string constant pool, which already contains the object, and returns it (but with no variable to receive it).
T2 = “1”.
This will look for the object “t” in the string constant pool, which already has that object, and return it to T2, which points to the object in the constant pool.
Can an object in the constant pool and an object in the heap be equal? so
T1 is not equal to T2.
Then the following
t3 = new String(“2”) + new String(“2”);
The string constant pool has a “2” object before this code is called. After this code is executed, it will actually call the Append () method class of StringBuilder to concatenate and create a “22” object in the heap. Note that the literal doesn’t have the string “22” at this point. That is, there is no “22” object in the string constant pool. At this point t3 points to the object “22” in the heap
t3.intern();
After executing this method
In JDK1.6, it did not find an object with content “22” in the string constant pool, so it added the object to the string constant pool and returned it (there was no variable to receive the returned object). Note that you are adding an object, not a reference.
T4 = “22”.
This code returns the “22” object in the string constant pool, in which case T4 refers to the object in the string constant pool.
Obviously, one object in the string constant pool and one in the heap are not the same object, so in JDK1.6, t3 and t4 are not equal.
But in JDK1.7
T3.intern () assigns a reference to that object in the heap to the string constant pool, since there is no object with content “22” in the string constant pool. Notice that the string constant pool holds a reference to this object in the heap.
T4 = “22”.
After executing this line, what is returned to T4 from the string constant pool are references to objects in the heap. At this point t4 actually refers to a reference to an object in the heap, that is, T3 and T4 refer to the same object.
So T3 is equal to T4.
I don’t know if you understand? It took me a long time to figure it out…
Question 2
As for question 2, I’m just going to cover the second half of the code, and if you understand the first half of the code, then question 2 is almost natural.
String t3 = new String("2") + new String("2");
String t4 = "22";
t3.intern();
System.out.println(t3 == t4);
Copy the code
T3 = new String(“2”) + new String(“2”).
This code starts with a “2” object in the string constant pool, and after execution, it actually calls the Append () method class of StringBuilder for concatenation, and finally creates a “22” object in the heap. At this point t3 points to the object “22” in the heap
T4 = “22”.
The object “22” already exists in the string constant pool, so this object is returned directly to T4, which points to an object in the string constant pool.
T3 and T4 must not be the same object. (t3. Intern)
Question 3
public static void main(String[] args){
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
System.out.println(c == d);
System.out.Println(e == f);
System.out.println(c == (a + b));
System.out.println(c.equals(a+b));
System.out.println(g == (a + b));
System.out.println(g.equals(a + b));
}
Copy the code
Let me give you a quick answer to this question.
(1). There is a constant pool of Java primitive types encapsulating classes in memory. These classes include Byte, Short, Integer, Long, Character, and Boolean. Note that Float and Double do not have corresponding constant pools.
(2). The wrapper classes for the above five integers can only use the constant pool if the object value is between -128 and 127.
(3). In the virtual machine zhi-ming zhou have such a words: “= =” operation of the wrapper class operator in the case of not meet arithmetic does not automatically split open a case, as well as their equals () method does not deal with the relationship between data types, if there are any “= =” can be deduced arithmetic are both side will automatically split open a case and the data type conversion processing.
(4).Long equals first checks if it is of type Long.
(5). Whether Integer or Long, their equals method compares values.
So:
System. The out. Println (c = = d).
Because of the constant pool, c and D refer to the same object (note that == is comparing objects, i.e. addresses, not values). So to true
System. The out. Println (e = = f).
Since 321 exceeds 127, the constant pool is disabled, so e and f are the same value, but not the same object, so false.
System. The out. Println (c = = (a + b)).
In this case, there are arithmetic operations on both sides of ==, which will be unpacked, so we are comparing values, not objects. Therefore true.
System.out.println(c.equals(a+b))
The value of c is the same as that of a+b and is true.
System.out.pirnln(g == (a + b))
Because of the arithmetic operation on both sides of ==, the comparison is numeric and therefore true.
System. The out. Println (g.e quals (a + b)).
Long equal checks whether a+b is Long or not, and therefore false
This is the end of the problem, the above is their own understanding, if there is something wrong with the above, you are very welcome to give advice.
To the end.
If you are used to reading technical articles in the wechat public number want to get more resources students welcome to pay attention to my public number: helpless pain code farmers update the article every week, absolutely let you gain do not believe you try