The data types in Java can be divided into two categories:
1. The basic data type, also known as the original data type. Byte,short,char,int,long,float,double,boolean
The comparison between them, applying the double equals sign (= =), compares their values.
2. Composite data type (Class)
When they compare with (= =), they compare the storage address in memory, so unless it is the same new object, their comparison result is true, otherwise the result is false. All classes in Java are inherited from the base class of object, and a method of equals is defined in the base class in object, and the initial behavior of this method is to compare the memory address of the object, but in some class libraries This method is overwritten, such as String,integer, In these classes, date equals has its own implementation, rather than the comparison class's storage address in heap memory.
For the equals comparison between composite data types, the comparison between them is based on the address value of the location in memory where the Equals method is not covered, because the Equals method of object is compared with the double equals sign (= =). So the result of the comparison is the same as the double equals sign (= =).
When comparing variables with "= =" in Java, the system uses the values stored in the "stack" of variables as the basis for comparison.
The base data type is stored in the stack as its content value, and the object type is the address that is stored in the stack, which points to the object in the heap.
The object class in the Java.lang package has the Publicboolean equals (Object obj) method, which compares two objects for equality.
The Equals method of other objects returns true only if the object contents of the two references to the comparison are the same.
In short, "= =" compares the address. You can also think that "= =" compares the object handle, while Equals () compares the object content. Or, "= =" compares the contents of "stack", while Equals () compares the contents of "heap".
The difference between equals and = = in Java