When I was investigating the equivalence relation, Java equals came out, so make a note
-== is false --true with equals
I think you know well when it becomes. This is the time when they are different instances and have the same value.
vice versa
-== is true --false with equals
I don't think the case of becoming is very familiar.
This time we will verify the matter.
environment: Java1.8 Eclipse Neon.1
python
@org.junit.Test
public void test() {
boolean b1 = 1 == 1.0;
Integer i2 = 1;
boolean b2 = i2==1.0; //true
boolean b3 = i2.equals(1.0); //false
System.out.println(b2);
System.out.println(b3);
}
Double # valueOf is called to auto boxing a double type 1.0 to a double type.
auto-boxing
public static Double valueOf(double d) {
return new Double(d);
}
It will be false because it will be a comparison using equals between 1.0 converted to Double type and 1 of Integer type.
Integer.equals()
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
Java Puzzle has a similar problem with auto boxing.
https://www.youtube.com/watch?v=V1vQf4qyMXg&t=11m19s
auto boxing can give you what we call the surprise left jab
by Joshua Bloch
Recommended Posts