The difference between equals and = = in Java
The data types in Java can be grouped 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 applies the double equals sign (= =) and compares them to their values.
2. Composite data type (Class)
When they compare with (= =), they compare their store address in memory, so unless the same object is new, their comparison results to 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,date in these classes equals has its own implementation, and is no longer the address of the comparison class in heap memory.
For equals comparisons between composite data types, the comparison between them is based on the address value of their place in memory, without having to overwrite the Equals method, because the Equals method of the object is also compared with the double equal sign (= =). So the result of the comparison is the same as the result of the double equal sign (= =).
The difference between Java equals and =,==
The difference between = = and equals
1. = = is operator
2. Equals is the method of a String object
There are generally two types of comparisons
1. Comparison of basic data types
2. Comparison of referenced objects
1. Comparison of basic data types
= = and equals compare whether the values are equal, the equality is true, or false
2. Comparison of referenced objects
= = and equals are compared to whether the address in the stack memory is equal, the equality is true, or false
Attention:
1. String is a special reference data type, = = comparison is the reference address of the string object is the same, equals compares the contents of 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 address of SS and SSS in the stack is different, but the contents in the heap are the same.
string ss = new String ("abc");
String ssss = SS;
Determine if the reference address of SS and SSSs in the stack is the same if
(ss = ssss) {
System.out.println ("ss = = SSSs");
} else{
System.out.println ("SS!= ssss");
}
Determine if the contents of SS and SSSs are the same in the heap 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 indicates that SS and SSSs are the same objects and that they are in the same content in the heap
2. Comparison of referenced 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 that the reference address in the stack is different
Obj1 and Obj3 are the same objects, with the same reference address in the stack
Two. = difference with equals = =
= the value assigned to the right is assigned to the variable on the left. equals and = = are actions
Thank you for reading, I hope to help you, thank you for your support for this site!