The difference between equals and == in Java
Data types in java can be divided into two categories:
1. Basic data type, also known as primitive data type. byte,short,char,int,long,float,double,boolean
The comparison between them, applying a double sign (==), is compared to their values.
2.Compound data type (class)
When they compare with (==), they compare their storage address in memory. Therefore, unless they are objects from the same new, the result after comparison is true, otherwise the result after comparison is false. All classes in JAVA inherit from the base class Object. A method of equals is defined in the base class in Object. The initial behavior of this method is the memory address of the comparison object, but in some class libraries, this method is overwritten. For example, String, Integer, and Date have their own implementation in these classes, and are no longer the storage address of the comparison class in heap memory.
For equals comparison between composite data types, without overwriting the equals method, their comparison is based on the address value of their storage location in memory. Because the equals method of Object is also compared with a double equal sign (==), so the result after 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 the 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 compare whether the values are equal, if equal, it is true, otherwise it is false
2. Comparison of reference objects
== and equals are both compared to whether the addresses in the stack memory are equal. If equal, it is true, otherwise it is false.
Notice:
1. String is a special reference data type. == compares whether the reference address of the string object is the same, and equals compares whether the content in the stack is 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 that the memory addresses of ss and ss in the stack are different, but the contents in the heap are the same.
String ss = new String("abc");
String ssss = ss;
//Judge 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"); }//Judge 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 objects and that they have the same content in the heap
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
It means that obj1 and obj2 are not the same object, and the reference address in the stack is different
obj1 and obj3 are the same objects, and the reference address in the stack is the same
2. The difference between = and equals ==
= represents assignment, that is, assign the value on the right of = to the variable on the left. equals and == are the operations
Thank you for reading, I hope it can help you. Thank you for your support for this site!