Equal and = = Personal notes

Source: Internet
Author: User

First look at the following figure, see clearly the figure we want to talk about the knowledge point is to speak clearly half


int a=10;
String b= "SS";
String C=new string ("KKK");
Person D=new person ("DLF");
String E=new string ("KKK");
String f= "SS";


There are heap heaps, stack stacks, and constant pool constant in the JVM.
Our basic objects, including int,double, are placed on the stack, and reference types such as String,person (person is a Pojo class) are referenced (as in the c,d above) on the stack, and the real data is placed in the heap.


= = Compare whether the data stored in the stack is the same for two objects
B==f is a two 105 address (105 means SS in the constant pool address)
C==e a 10,631 A 1095 nature is not equal


The contents of the comparison of equals
C.equsls (e) is the two KKK in the comparison of the answer is self-evident


Objeck equals method such as the following
public boolean equals (Object obj) {
return (this = = obj);
}
In other words the = = of the class of object and equals is the same thing
Interested in being able to look at the Equals method of string


about = = and equals are finished, say again the construction of the string
There are two types of strings that we use most often
String a1= "BBB";
String A2=new string ("CCC");
There are two different ways of
The first way, through the above figure, we also see, BBB this data is stored in the constant pool, the exact word is to use string a1= "BBB" in such a way to produce a string, the JVM first in the constant pool to find whether there is BBB this data, false with the BBB this data address to A1, Otherwise, create a new data unit in the constant pool to store the BBB, and then give the address of the unit to A1.

(Two of the 105 in this example)
Another way is that at compile time there will be a CCC in the constant pool, wait for execution, will be in the heap area in the creation of a data unit to store the CCC, and then A2 inside the heap inside the data unit of the address! And even if this string is already in the heap, by means of new, It will also generate a heap of data (for example, 1063 and 1095)


So
String A=new string ("BBB");
Produces two objects, one in the heap and one in the constant pool.




String A=new string ("BBB");
String C=new string ("CCC");
Produces four objects, two in the heap two in a constant pool.


String A1=new string ("SSS");
String A2=new string ("SSS");


In this case, there must be two objects in the heap area, what about the constant pool?

One SSS or two SSS I'm not sure, but I'm expecting two.

Equal and = = Personal notes

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.