Comparison of equality of raw types is different from comparison two objects. If the value 5 is stored in two different int variables, compare whether the results will result in the result of the two variables as the Boolean value true:
Public class testintcomparison {
Public static void main (String [] args) {
INT x = 5, y = 5;
System.out.println (
"x == y yields" (x == y));
}
}
TestintComparison produces the following output:
D: /> Java TestintComparison
x == y Yields True
The equal operator is compared to their values due to the original type. The object's reference is compared to the object is not the actual content of the object. You may ask: "Are these references to the same object?" For the explanation of clear, please see another version of the DOG of Tag and Age:
Class Dog {
Int tag;
Int agec;
Public void settag (int T) {tag = t;
Public void setage (int a) {agn = a;
}
If there are two dogs (DOG), even if their content is the same, they are not equal with == operators. The output of the following code segment indicates that A and B are not equal in using "==":
DOG A = New Dog ();
A.SETTAG (23129);
A.SETAGE (7);
DOG B = New DOG ();
B.SETTAG (23129);
B.SETAGE (7);
IF (a == b) {
System.out.println ("a is equal to b");
}
Else {
System.out.println ("a is not equal to b");
}
So how should I compare the values of the two objects instead of comparing their references? The Java (TM) programming language has an agreement, and method equals () is used to define the number of object values. Method Equals () is defined in class Object, and if it is not overloaded in its subclasses, it is used by default. In order to compare the value of both dogs (DOG) A and B, you should rewrite the above comparison section:
IF (a.equals (b)) {
System.out.println ("A is equals () to b");
}
Else {
System.out.println ("A is not equals () to b");
}
In the above code, if there is no overloading Equals () method in the DOG, two dogs still don't wait. Because Object.Equals () actually simulates the function of the == operator. The definition of equals () in the Dog is very good:
Class Dog {
Int tag;
Int agec;
Public void settag (int T) {tag = t;
Public void setage (int a) {agn = a;
Public Boolean Equals (Object O) {
DOG D = (DOG) O;
IF (tag == d.tag && age == D.AGE) {
Return True;
}
Return False;
}
}
Why is equals () parameter types of Object instead of DOG? Because you are the method equals () that is overloading the parent class Object (), you must mark it with the same method. However, the parameters we wish to pass are another DOG, so in order to access the parameters, it needs to be converted to DOG. However, because equals () is defined in the Dog, you must check if the incoming object is a Dog, because some people may use: FIDO.EQUALS ("blort");
String "bloth" is also an object, so the tag of equals () in the Dog is matched. Equals () The correct way of writing is:
Public Boolean Equals (Object O) {
IF (o InstanceOf Dog) {
DOG D = (DOG) O;
IF (tag == d.tag && age == D.AGE) {
Return True;
}
}
// false if not dog or contents mismatched
Return False;
}
Operator InstanceOf inquiry O is whether it is an instance of DOG (subclass of DOG). The comparison of the string introduces the last problem of the object comparison, that is, "ABC" == "DEF" expression value true or false? It is False because they are different objects (obvious, their content is different). However, the following expression "ABC" == "abc" is TRUE or false? Unfortunately, this is determined by the compiler. If the compiler will optimize the two references to "ABC" into an object rather than two objects, the value of the expression is TRUE. However, if the compiler does not do this, the value of the expression should be false! If you really want to judge that two strings are physically do not physically, use equals () methods:
Boolean b = "abc" .equals ("def"); // false
Boolean c = "abc". Equals ("abc"); // true