Equal and = personal notes

Source: Internet
Author: User

Tag: Stack heap String object

First of all, let's take a look at the figure below. We can see clearly what we want to talk about.


Int A = 10;
String B = "SS ";
String c = new string ("KKK ");
Person d = new person ("DLF ");
String E = new string ("KKK ");
String F = "SS ";


The JVM contains heap, stack, and constant pool constant.
Our basic objects, including int and double, are all placed in the stack. reference types such as string and person (person is just a pojo class) References (c, d above) put the data in the stack, and put the real data in the heap.


= Compare whether two objects store the same data in the stack
B = f compares two 105 addresses (105 refers to the SS address in the constant pool)
C = e a 1063 A 1095 is naturally not equal


Content in the heap of equals comparison
C. equsls (e) is the self-evident comparison between the two KKK.


The equals method of objeck is as follows:
Public Boolean equals (Object OBJ ){
Return (this = OBJ );
}
In other words, the = of the object class is the same as equals.
If you are interested, you can check the string equals method.


About = and equals, let's talk about string construction.
We usually use two types of string construction.
String a1 = "BBB ";
String a2 = new string ("CCC ");
The two methods are different.
First, we can see from the figure above that the BBB data is stored in the constant pool; specifically, the string a1 = "BBB" is used to generate a string, the JVM first checks whether the BBB data exists in the constant pool. If yes, it returns the BBB data address to A1; otherwise, it creates a new data unit in the constant pool to store BBB, then, the address of the unit is given to A1. (The two 105 in this example)
The second method is to have a CCC in the constant pool during compilation. When running, it will store the CCC in a data unit created in the heap area, then A2 stores the address of the data unit in the heap! In addition, even if the heap already has this string, the new method will generate another heap data (such as 1063 and 1095)


So
String A = new string ("BBB ");
Two objects are generated, one in the heap and the other in the constant pool.


String A = new string ("BBB ");
String c = new string ("CCC ");
Generates four objects, two in the heap and two in the constant pool.


String a1 = new string ("Sss ");
String a2 = new string ("Sss ");


In this case, the heap must have two objects. What about the constant pool? I'm not sure if one SSS is still two SSS, but I guess it should be 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.