In recent work, it is often necessary to compare the values of 2 objects. There is a problem often encountered, is the following 2 kinds of situations.
public static void Main (string[] args) {
Integer a = 11;
Object B=a;
System.out.println (B.equals ("one"));//false
String C = "11";
Object d = c;
System.out.println (D.equals ("one"));//true
}
To view the JDK document in Chinese:
equals of Integer
Compares this object with the specified object. When and only if the parameter is notnull, and is an object that contains the sameintValue ofIntegerobject, the result istrue。
equals of String
Compares this object with the specified object. When and only if the parameter is notnull, and is an object that contains the samestringValue ofStringobject, the result istrue。
Both string and integer overload the Equals method of object, which requires that two object comparisons must first be of the same type.
This type should refer to the "actual type", the actual type of Object D is "string", so the Equals method of string is called.
Multi-State ~
Original starting: http://fansunion.cn/article/detail/536.html
Equals in Java compares, pits one