What is the comparison between equals and = =?
1. Boolean tem = a = = B;
First = = = Comparison is definitely the address, from the point of view of the stack is that = = = Compare the contents of the stack. Because the stack is used to hold the address or the literal value of an automatic variable in the eight basic types of Java (an automatic variable is a variable that is defined with int a = 1; If it is an automatic variable comparison value, it must be compared with = =, because Equals () is a method, so it must be invoked by the object to be used for comparison. An automatic variable is neither an instance of a class nor a reference to a class, so you cannot use the Equals () method.
2.boolean tem = a.equals ("B");
The Equals () method is generally used to compare the contents of an object, but there are also cases where you can compare the addresses of two objects.
Next
Write a test program
Package com;
Import Java.util.Date;
public class Test {public static void main (string[] args) {integer integer1 = new Integer (1);
Integer integer2 = new Integer (1);
String str1 = new String ("123");
String str2 = new String ("123");
Date date1 = new Date ();
Date Date2 = new Date ();
Double double1 = new Double ("1.0");
Double double2 = new Double ("1.0");
Boolean tem1 = new Boolean (true);
Boolean tem2 = new Boolean (true);
Object Object1 = new Object ();
Object object2 = new Object ();
SYSTEM.OUT.PRINTLN ("----Object------");
System.out.println (Object1.equals (object2));
System.out.println (Object1 = = Object2);
System.out.println (Object1.equals (Object1));
System.out.println (Object1 = = Object1);
SYSTEM.OUT.PRINTLN ("----Boolean------");
System.out.println (Tem1.equals (TEM2));
System.out.println (tem1 = = tem2);
SYSTEM.OUT.PRINTLN ("----Double------");
System.out.println (Double1.equals (double2)); System.oUt.println (Double1 = = Double2);
SYSTEM.OUT.PRINTLN ("----Integer------");
System.out.println (Integer1.equals (integer2));
System.out.println (Integer1 = = Integer2);
SYSTEM.OUT.PRINTLN ("----String------");
System.out.println (Str1.equals (str2));
System.out.println (str1 = = str2);
SYSTEM.OUT.PRINTLN ("----Date------");
System.out.println (Date1.equals (date2));
System.out.println (date1 = = Date2);
}
}
Results:
----Object------
False
False
True
True
----Boolean------
True
False
----Double------
True
False
----Integer------
True
False
----String------
True
False
----Date------
True
False
First comparison of object, when the two objects are compared, the result of = = and Equals () is true, and when two objects are different, all returns FALSE. So when = = and equals are used to compare objects, they are compared to the address of the object, in fact, the essence is the same. The following is the code for the Equals () method in the object class:
public boolean equals (Object obj) {return
(this = = obj);
}
And for boolean,double (float the same), Interger (Shot,long the same), string,date I also find their source code, the following I posted in turn in Boolean,double,interger,string, Date the Equals () method's source code in these classes, at which point the Equals () methods are rewritten because these classes inherit from the object class.
Boolean:
public boolean equals (Object obj) {
if (obj instanceof Boolean) {return
value = = ((Boolean) obj). Booleanvalue (); c3/>} return
false;
}
Double:
public boolean equals (Object obj) {return
(obj instanceof Double)
&& doubletolongbits ((Double) obj) . value) = = =
Doubletolongbits (value));
}
Interger:
public boolean equals (Object obj) {
if (obj instanceof Integer) {return
value = = ((Integer) obj). Intvalue ();
} return
false;
}
String:
public boolean equals (Object anobject) {
if (this = = AnObject) {return
true;
}
if (anobject instanceof string) {
string anotherstring = (string) anobject;
int n = value.length;
if (n = = anotherString.value.length) {
char v1[] = value;
Char v2[] = Anotherstring.value;
int i = 0;
while (n--! = 0) {
if (V1[i]!= v2[i]) return
false;
i++;
}
return true;
}
return false;
}
Date:
public boolean equals (Object obj) {return
obj instanceof Date && getTime () = = ((Date) obj). GetTime ();
}
In other words, the Equals () method of the object class is used to compare the actual content of the two objects instead of the address at these times, and certainly not just that, but just a few of the more common Java primitive classes that override the Equals () method of the object class.
Thank you for reading, I hope to help you, thank you for your support for this site!