显然,您自己没有尝试过,因为,<
和不适用于对象。>
<=
>=
但是,==
比较左右操作数。当它们的二进制相同时,结果为真。对于对象,in 比较指针。所以这意味着只有当对象在内存中左右是同一个对象时,这才会导致 true。
其他方法,如 compareTo 和 equals 提供了一种自定义方法来比较内存中的不同对象,但它们可能彼此相等(即数据相同)。
如果是字符串,例如:
String str0 = new String("foo");
String str1 = new String("foo");
// A human being would say that the two strings are equal, which is true
// But it are TWO different objects in memory. So, using == will result
// in false
System.out.println(str0 == str1); // false
// But if we want to check the content of the string, we can use the equals method,
// because that method compares character by character of the two objects
String.out.println(str0.equals(str1)); // true
String.out.println(str1.equals(str0)); // true