根据以下代码及其程序的输出结果,可以发现一点equals和==的使用场景。
==的使用场合:
equals的使用场合
错误的使用场合
代码:
package com.baby.javaStudy.tij4;
//定义一个新的类。不去覆盖Object.java中的public boolean equals(Object obj)方法。
class IntValue {
public IntValue(int i) {
super();
this.i = i;
}
int i;
}
//定义一个新的类。重新定义public boolean equals(Object obj)。
class CharValue {
char c;
public CharValue(char c) {
super();
this.c = c;
}
public boolean equals(CharValue charValue) {
return this.c == charValue.c;
}
}
public class EqualTest {
public static void main(String[] args) throws NullPointerException {
System.out.println("==========Integer test===================");
Integer integer1 = new Integer(10);
Integer integer2 = new Integer(10);
Integer integer3 = new Integer(20);
Integer integer4 = integer1;
System.out.println("new Integer(10) == new Integer(10) is\t" + (integer1 == integer2));
System.out.println("integer1.equals(integer2) is\t" + integer1.equals(integer2));
System.out.println("integer1.equals(integer3) is\t" + integer1.equals(integer3));
System.out.println("integer1 == integer4 is\t" + (integer1 == integer4));
System.out.println("=========IntValue test====================");
IntValue intValue1 = new IntValue(10);
IntValue intValue2 = new IntValue(10);
IntValue intValue3 = intValue1;
System.out.println("new IntValue(10) == new IntValue(10) is\t" + (intValue1 == intValue2));
System.out.println("intValue1.equals(intValue2) is\t" + intValue1.equals(intValue2));
System.out.println("intValue1 == intValue3 is\t" + (intValue1 == intValue3));
System.out.println("==========CharValue test===================");
CharValue charValue1 = new CharValue('a');
CharValue charValue2 = new CharValue('a');
System.out.println("new CharValue('a') == new CharValue('a') is\t" + (charValue1 == charValue2));
System.out.println("charValue1.equals(charValue2) is\t" + charValue1.equals(charValue2));
System.out.println("=============================");
try {
EqualTest equalTest = null;
if (equalTest == null) {
System.out.println("equalTest == null");
}
if (equalTest.equals(null)) {
System.out.println("equalTest.equals(null)");
}
} catch (NullPointerException e) {
System.out.println("equalTest.equals(null) exception");
} catch (Exception e) {
System.out.println("other exception");
}
System.out.println(0.99999999999999999999f == 1.00000000000000000001f);
System.out.println(
Float.floatToIntBits(0.99999999999999999999f) == Float.floatToIntBits(1.00000000000000000001f));
System.out.println(0.99999f == 1.00001f);
System.out.println(Float.floatToIntBits(0.99999f) == Float.floatToIntBits(1.00001f));
System.out.println(0.9999999f == 1.0000001f);
System.out.println(Float.floatToIntBits(0.9999999f) == Float.floatToIntBits(1.0000001f));
}
}
结果:
new Integer(10) == new Integer(10) is false
integer1.equals(integer2) is true
integer1.equals(integer3) is false
integer1 == integer4 is true
=============================
new IntValue(10) == new IntValue(10) is false
intValue1.equals(intValue2) is false
intValue1 == intValue3 is true
=============================
CharValue('a') == CharValue('a') is false
charValue1.equals(charValue2) is true
=============================
equalTest ==null
equalTest.equals(null) exception
true
true
false
false
false
false
结论:
==的使用场合:
- 除去浮点数之外的基本数据类型的比较;
- 两个浮点数的比较,一般的做法是将两数相减。如果结果等于一个“比较小”的值,那就视为相等。
- 一个对象的引用与null的比较;
- 一个对象的两个不同引用的比较;
equals的使用场合
- 实现了equals()方法的类的两个对象的比较。
错误的使用场合
- 使用==对一个类的两个对象的引用进行比较;此时的结果为false,因为,他们是“两个不同的对象”。