Talk about the constant pools that have blinded me all night.

Source: Internet
Author: User
Tags arithmetic

Before writing, we will look at a few questions, if you have a good understanding of these issues, it can not read this article, if you do not understand, then you can look at 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);}

Answer output:
JDK1.6 is false false
JDK1.7 is false true;
Question 2 (Swap the statement of question 1 for the position)

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);}

Answer output:
False false
For these two questions, read a few people's blog, can be described as flourishing, the more you see the more confused 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));}

Answer output:
True
False
True
True
True
False
Question 4:

运行时常量池与字符串常量池是什么关系?包含?

Before we solve the problem, let's take a brief look at some of the knowledge points of some constant pools (most of which come from Zhou Zhiming's deep Java Virtual machine).

Several constant pool 1.class file constant pools in the JVM

In the class file, in addition to the class version, fields, methods, interfaces and other descriptive information, there is also a message is Chang (Constant Pool Table), for the compilation period generated by the various literal and symbolic references.

Here is a simple explanation of the literal and symbolic references
Literal quantity
Literals are similar to the constants we normally say, mainly including:

    1. Text string: A string that we can see in the code, such as String a = "AA". where "AA" is the literal amount.
    2. A variable that is final modified.

Symbol reference
The main include the following constants:

    1. Classes and interfaces and fully qualified names: for example, for the class String, its fully qualified name is java/lang/string.
    2. Field name and descriptor: the so-called field is a variable declared in a class or interface, including class-level variables (static) and instance-level variables.
    3. The name and descriptor of the method. The so-called descriptor is equivalent to the parameter type of the method + the return value type .
2. Run a constant-rate pool

As we said above, the constant pool in the class file, which will load the run-time pool in the method area When the classes are loaded. It is also important to note that the run-time-constant pool is globally shared, and multiple classes share a single run-time-constant pool. And there is only one copy of the constant pool in the class file where multiple identical strings are running.
Note the run-time constant pool exists in the method area .

3. String constant Pool

Look at the name. The string constant pool is used to hold the string , meaning that the literal string in the constant pool enters the string constant pool when the class loads.
What is the relationship between a string constant pool and a run-time pool? Above, we say that the literal in the constant pool will be in the class load backward into the run-time constant pool, where the literal string is included, obviously from this text we can know that the string constant pool exists in the run-time pool. is also present in the method area .
But in Zhou Zhiming's deep Java virtual machine, it is said that when the JDK1.7, the string constant pool is moved out of the method area, transferred to the heap .
Then we can infer that, in the JDK1.7 and later versions, the run constant pool does not contain a string constant pool, the run-time pool exists in the method area, and the string constant pool exists in the heap .

Having said so much, now we begin to solve the problem raised above.

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);}

Answer output:
JDK1.6 is false.
JDK1.7 is false true;
Before we solve this problem, let's take a look at the questions that are often asked in another interview.

String t = new String("tt");

If there is only such a line of code in the program, how many objects does this line of code create?
As we said above, "TT" is a literal, then it will exist in the string constant pool after the class is loaded, that is, the string constant pool has created the string object "TT" before the code of string t = new string ("tt") is executed, as we all know, New this 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 the following code create?

String t1 = new String("tt");String t2 = new String("tt");

Answer is that this code creates three objects, which we have said, the string constant pool will only save a copy of the same content 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 were executed and two were created in the heap, three were created altogether.
So what are the next pieces of code that create several objects?

String t = "tt";

Answer is 1, before this code executes, the string constant pool has created a "TT" object, but because this line of code is not a new method, so the virtual opportunity in the string constant pool to find whether the content is "TT" string object, if there is, then directly return the reference to the string, So the end result creates only one object.
Back to our question, here we first explain the Intern method of string .


For example we called T.intern ().
in JDK1.6, after invoking this method, the virtual opportunity in the string constant pool is looking for the object with the content equal to "TT", and if so, returns the object, if not, in the string constant pool this object. Note that this object is added to the string constant pool.
After JDK1.7, if the Intern method is called, the virtual opportunity in the string constant pool is looking for the object with the content equal to "TT", and if so, returns the object if it does not. The copy. Note that this time you are adding a reference to the object in the heap.


now start to parse the code in question
T1 = new String (" 1 ").
Before this code executes, the string constant pool already has the object "T", and after execution will also create a "T" object in the heap, at this point T1 is pointing to the object in the heap .
T1.intern ();
After this code executes, the object in the string constant pool is searched for "T", the object is already present in the string constant pool, and the object is returned (but no variable is received after the return).
T2 = "1".
This sentence executes after the string constant pool looks for the object content "T", the string constant pool already has this object, returned to T2, at this point T2 to the object in the constant pool .
One is an object in a constant pool, and one is an object in the heap, can the two be equal? Therefore,
T1 is not equal to T2.


Then the following
T3 = new String ("2") + New String ("2");
Before this code call, the string constant pool has an object of "2", which, after execution, actually calls StringBuilder's append () method class for stitching, and finally creates a "22" object in the heap, noting that the literal does not have "22" at this time. This string, which means that there is no "22" object in the string constant pool. at this point, T3 points to the "22" object in the heap .
T3.intern ();
After executing this method


in JDK1.6 , it did not find an object in the string constant pool that was "22", so this time it would add the object to the string constant pool and return the object (there is no variable to receive the returned object at this point). Note that you are adding an object, not a reference.
T4 = "22".
After this code executes, it returns a string constant in the pool that contains the "22" object, at which point the T4 points to the object in the string constant pool .
Obviously, an object in a string constant pool, one in the heap, two objects is not the same object, so at the time of JDK1.6, T3 and T4 are not equal.


But at the time of JDK1.7
After T3.intern () is executed, the reference to the object in the heap is assigned to a string constant pool because there is no object in the string constant pool that has a content of "22". Note that at this point the string constant pool holds a reference to this object in the heap .
T4 = "22".
After executing this code, the reference to the object in the heap is returned to T4 from the string constant pool. At this point T4 is actually referring to the objects in the heap, meaning that T3 and T4 point to the same object.
So t3 and T4 are equal.
I don't know, do you understand? Anyway, I've been doing it for a long time to understand ...

Question 2

As for question 2, I'll just talk about the next half of the code, the upper half if you understand the question 1, then question 2 is almost naturally understood.

String t3 = new String("2") + new String("2");String t4 = "22";t3.intern();System.out.println(t3 == t4);

T3 = new String ("2") + New String ("2").
Before this code call, the string constant pool has an object of "2", which, after execution, actually calls StringBuilder's append () method class for stitching, and finally creates a "22" object in the heap. at this point, T3 points to the "22" object in the heap .
T4 = "22".
Before this code executes, the string constant pool already has the object "22", all directly returning this object to T4, when T4 points to the object in the string constant pool .
So T3 and T4 certainly not the same object Ah, t3.intern this sentence can almost ignore, will not give T3 and t4 any impact.

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));}

For this question, let me briefly say that you may understand.

(1). There is a constant pool of Java basic types encapsulating classes in memory. These classes include
Byte, short, Integer, Long, Character, Boolean. It is important to note that the two classes of float and double do not have a corresponding constant pool.


(2). The above 5 types of integer wrapper classes are only available when the object value is -128~127 to use these constant pools.


(3). In the Zhou Zhiming of the virtual machine there is such a sentence: packaging class
the "= =" Runner does not automatically unboxing without encountering arithmetic operations, and their equals () method does not handle the relationship of the data type, and can infer that if an arithmetic operation is encountered on either side of "= =", it will be automatically disassembled and data type conversion processed.


(4). The Equals method of long will first determine if it is a long type.


(5). Either integer or long, their equals method compares the values.


So:
System.out.println (c = = d).
Because of the constant pool, C and D point to the same object (Note that the = = comparison is the object, that is, the address, not the value). therefore true
System.out.println (E = = f).
Because 321 exceeds 127, the constant pool loses its function, so the E and F values are the same, but not the same object, which is false.
System.out.println (c = = (A+b)).
At this point = = There are arithmetic operations on both sides, unpacking is done, so at this time the value is compared, not the object. Therefore, it is true.
System.out.println (C.equals (a+b))
The values of C and a+b are equal to true.
SYSTEM.OUT.PIRNLN (g = = (A + b))
Because the = = has arithmetic operations on both sides, the comparison is numeric and therefore true.
System.out.println (G.equals (a+b)).
A long type of equal in comparison is the time, will first determine whether A+B is a long type, obviously a+b is not, so false

This is the end of the problem, the above is their own understanding, above if there is something wrong, very welcome to your guidance.
End .

If you think the article is good, may wish to order a praise to go again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.