Starting with Java equals and =

Source: Internet
Author: User

How do I arrange objects when the program is running? How is the memory allocated? This understanding is of great help to our understanding of program running. Data can be stored in five places:

1Memory. This is the fastest storage zone, because it is located in a different place from other storage areas-inside the processor. However, the number of registers is extremely limited, so registers are allocated as needed.

2Stack. Stack is short for Stack, which is located in general-purpose Ram, but can be directly supported from the processor through the stack pointer. If the stack pointer moves down, new memory is allocated. If the stack pointer moves up, the memory is released. This is a fast and effective distribution storage method, second only to registers.Java object references are stored in the stack..

3Heap). A general memory pool (also exists in RMA ),Used to store Java objects. The advantage of heap is that the compiler does not need to know how long the stored data will survive in the heap. Therefore, it is flexible to allocate storage in the heap. When you need an object, you only need to write a new line of simple code. When you execute this line of code, it will automatically store and allocate it in the heap. It may take more time to allocate and clean up storage with stacks than to allocate storage with stacks.

4Constant Storage. Constant values are usually stored directly inside the program code. This is safe because they will never be changed.

5Non-RMA Storage. If the data exists completely outside the program, it may not be controlled by the program and may exist even if the program is not executed. Two basic examples are stream objects and persistent objects. In a stream object, cash is converted to a byte stream, which is usually sent to another machine. In persistent objects, objects are stored on disks, so they can be kept in their own State even if they terminate the program. The trick of this storage method is to convert an object into a thing that can be stored on other media. When needed, it can be restored to a conventional RMA-based object. Java provides support for lightweight persistence, while mechanisms such as JDBC and hibernate provide more complex support for storing and reading object information in databases.

The above concepts are mainly taken from thinking in Java. I personally think that the understanding of these concepts can help us better differentiate equals and =. The relationship between the two is described below:

= The operator is used to compare whether the values of two variables are equal, that is, to compare whether the values stored in the memory corresponding to the variables are the same, to compare two basic types of data, two reference types of data, or two reference variables, you can only use the = Operator.

If the data that a variable points to is of the object type, two pieces of memory are involved, the object occupies one piece of memory (heap memory), and the variable itself (here, the object reference) it also occupies one piece of memory (stack memory), for example, object OBJ = new object (); when the variable obj is a piece of memory, new object () is a memory. At this time, the value stored in the memory corresponding to the variable is the first address of the memory occupied by the object. If you want to compare two variables that point to the same object, it depends on whether the values in the memory corresponding to the two variables are equal, at this time, we need to use the = Operator for comparison.

The equals method can be used to compare whether the content of two independent objects is the same. It is like comparing two objects with the same looks.

Simply put:

= The operation compares whether the values of two variables are equal. For a referenced variable, it indicates whether the addresses of the two variables stored in the heap are the same, that is, whether the stack content is the same.

The equals operation indicates whether the two variables are referenced to the same object, that is, whether the content in the heap is the same.

= Compare the addresses of two objects, while equals compares the content of two objects.

The following are examples:

1. Equals and = 1 in string, public class teststring {public static void main (string [] ARGs) {string S1 = "Monday "; string S2 = "Monday" ;}} what are the objects in the above program? Check the program. modify the program public class teststring {public static void main (string [] ARGs) {string S1 = "Monday"; string S2 = "Monday "; if (S1 = S2) system. out. println ("S1 = S2"); else system. out. println ("S1! = S2 ") ;}} compile and run the program. Output: S1 = S2 Description: S1 and S2 reference the same string object --" Monday "! 2. if you change the program a little bit, you may find that: public class teststring {public static void main (string [] ARGs) {string S1 = "Monday "; string S2 = new string ("Monday"); If (S1 = S2) system. out. println ("S1 = S2"); else system. out. println ("S1! = S2 "); If (s1.equals (S2) system. out. println ("S1 equals S2"); else system. out. println ("S1 not equals S2") ;}} we use the new operator to create a program output: S1! = S2s1 equals S2 Description: S1 S2 references two "Monday" string objects respectively 3. the string buffer pool is originally created when the program is running. When the expression S2 = "Monday" is used to create a string, the program first searches for objects with the same value in the string buffer pool. In the first program, S1 is first placed in the pool. Therefore, when S2 is created, the program finds S1 with the same value and references S2 to the second program of the object Monday referenced by S1. It uses the new operator and tells the program clearly: "I want a new one! Don't be old! "So a new" Monday "sting object is created in the memory. They have the same value but different locations. One is swimming in the pool and the other is resting on the shore. Oh, it's a waste of resources. What should we do separately? 4. change the program again: public class teststring {public static void main (string [] ARGs) {string S1 = "Monday"; string S2 = new string ("Monday "); s2 = s2.intern (); If (S1 = S2) system. out. println ("S1 = S2"); else system. out. println ("S1! = S2 "); If (s1.equals (S2) system. out. println ("S1 equals S2"); else system. out. println ("S1 not equals S2");} added this time: S2 = s2.intern (); program output: S1 = s2s1 equals S2, (Java. lang. the intern () method of string "ABC ". the Return Value of the intern () method is still the string "ABC". On the surface, it seems that this method is useless. But in fact, it performs a small action: Check whether there is a string such as "ABC" in the string pool. If yes, the string in the pool is returned; if not, this method adds "ABC" to the string pool and returns its reference .) A better solution: intern () All strings to the buffer pool. It is best to perform this operation when new is used. String S2 = new string ("Monday "). intern (); then we can use = to compare the values of two strings. 2. Equals and = Java in the simple data type and encapsulation class provide an encapsulation class for each simple data type, each basic data type can be encapsulated into an object type. In addition to int (integer) and char (character), the first letter of other types is an encapsulation class name. Double (double), float (float), long (long), short (short), byte (byte), Boolean (Boolean ). INT and integer are used as examples to illustrate the differences between int and integer in Java: 1.int is the basic data type, and the default value can be 0; 2. integer is an int encapsulation class. The default value is null. Both 3.int and integer can indicate a certain value. 4.int and integer cannot be used with each other because they have two different data types; int a1 = 1; int a2 = 1; integer b1 = new INTEGER (1); integer b2 = new INTEGER (1); -------------------------------- a1 = a2 this is true, very simple, we all know that a1 = B1 is not valid. the values of the expressions are false, which are different data types. True in the previous version) b1 = B2, which is also not true. the expression value is false. Although it is of the same data type, they are two objects. = compares the addresses of two objects, and their addresses are not equal, if the content is equal to 1, b1.equals (B2) = true is true, and the expression value is true. for the same data type, two objects have different addresses and the content is the same. quals compares the content of two objects, so it is true. (A. Equals (B). Since equals compares two objects, neither a nor B can be of the basic data type. Otherwise, a compilation error occurs .) (In JDK and later versions, B can be the basic data type, but a cannot.) Similarly, Other encapsulation classes and basic types are also the same. in Java, the difference between equals and = compares the addresses of two objects, while equals compares the content of two objects. In JDK or later versions, the basic type and encapsulation class can be automatically converted, similar to the string type object and String constant. Integer I1 = 123; integer I2 = 123; int I = 123; integer I3 = new INTEGER (123); integer I4 = new INTEGER (123); system. out. println ("I1 = I2 =" + (I1 = I2); system. out. println ("i1.equals (I2) =" + (i1.equals (I2); system. out. println (); system. out. println ("I3 = I4 =" + (I3 = I4); system. out. println ("i3.equals (I4) =" + (i3.equals (I4); system. out. println (); system. out. println ("I2 = I4 =" + (I2 = I4); system. out. println ("i2.equals (I4) =" + (i2.equals (I4); system. out. println (); system. out. println ("I = I2 =" + (I = I2); system. out. println ("i1.equals (I) =" + (i1.equals (I); system. out. println (); system. out. println ("I = I4 =" + (I = I4); system. out. println ("i4.equals (I) =" + (i4.equals (I); ---------------------------- I1 = I2 = true i1.equals (I2) = true I3 = I4 = false i3.equals (I4) = true I2 = I4 = false i2.equals (I4) = true I = I2 = true i1.equals (I) = true I = I4 = true i4.equals (I) = true


As for the hashcode concept mentioned in many places, I think it is difficult to understand it. I will not discuss it here. Make a comparison when you really understand this part. Baidu Library in the article said more comprehensive, posted links for reference http://wenku.baidu.com/view/2d25e10d4a7302768e9939d7.html

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.