The difference between equals and == in Java
Data types in Java can be divided into two categories:
1. Basic data types, also called primitive data types. byte, short, char, int, long, float, double, boolean
To compare them, use the double equal sign (==), and compare their values.
2. Composite data type (class)
When they use (==) to compare, they compare their storage addresses in memory. Therefore, unless they are the same new objects, their comparison results are true, otherwise the comparison results is false. All classes in JAVA inherit from the base class Object. An equals method is defined in the base class in Object. The initial behavior of this method is to compare the memory addresses of objects, but in some class libraries This method has been overridden. For example, String, Integer, and Date have their own implementations of equals in these classes, instead of comparing the storage address of the class in the heap memory.
For equals comparison between composite data types, without overriding the equals method, the comparison between them is still based on the address value of their storage location in memory, because the equals method of Object also uses double The equal sign (==) is used for comparison, so the result of the comparison is the same as the result of the double equal sign (==).
The difference between java equals and =, ==
1. The difference between == and equals
1. == is an operator
2. equals is a method of String object
There are generally two types of comparisons
1. Comparison of basic data types
2. Comparison of reference objects
1. Comparison of basic data types
== and equals both compare whether the values are equal. If they are equal, it is true, otherwise it is false
2. Comparison of reference objects
== and equals both compare whether the addresses in the stack memory are equal. If they are equal, it is true, otherwise it is false
Note:
1. String is a special reference data type. == compares whether the reference addresses of string objects are the same, and equals compares whether the contents in the stack are consistent.
String ss = new String("abc"); String sss = new String("abc"); if(ss == sss){ System.out.println("ss == sss is true"); } if(ss.equals(sss)){ System.out.println("ss equals sss is true"); }
Console output:
ss != sss
ss equals sss
Indicates: ss and sss have different memory addresses in the stack, but the contents in the heap are the same.
String ss = new String(“abc”);
String ssss = ss;
//Determine whether the reference addresses of ss and ssss in the stack are the same if(ss == ssss){ System.out.println("ss == ssss"); }else{ System.out.println("ss != ssss"); } //Determine whether the contents of ss and ssss in the heap are the same if(ss.equals(ssss)){ System.out.println("ss equals ssss"); }else{ System.out.println("ss not equals ssss"); }
Console output:
ss == ssss
ss equals ssss
This shows that ss and ssss are the same object, and their contents in the heap are the same
2. Comparison of reference objects
TestBean obj1 = new TestBean(); TestBean obj2 = new TestBean(); TestBean obj3 = obj1; if(obj1 == obj2){ System.out.println("obj1 == obj2"); }else{ System.out.println("obj1 != obj2"); } if(obj1 == obj3){ System.out.println("obj1 == obj3"); }else{ System.out.println("obj1 != obj3"); }
Console output:
obj1!= obj2
obj1== obj3
Indicates that obj1 and obj2 are not the same object and have different reference addresses in the stack
obj1 and obj3 are the same object, and have the same reference address in the stack
2. The difference between = and equals ==
= represents assignment, that is, assigning the value on the right side of = to the variable on the left. equals and == represent operations
Thank you for reading, I hope it can help you, thank you for your support of this site!