Java 如果比较的两个对象都为null,equals方法应该返回什么?
我使用Eclipse自动工具为person对象实现equals方法。 如果两个人对象,Java 如果比较的两个对象都为null,equals方法应该返回什么?,java,null,equals,Java,Null,Equals,我使用Eclipse自动工具为person对象实现equals方法。 如果两个人对象,p1和p2为null,那么p1.equals(p2)应该返回什么? 当前实现提供了我的Eclipse返回True。但是,我尝试比较nullstring对象,它抛出NullPointerException。下面是代码。我还想知道为什么不同运行的代码会给出不同的结果。我将输出粘贴到下面 class Parent2 { private String name; private int id; public stati
p1
和p2
为null
,那么p1.equals(p2)
应该返回什么?
当前实现提供了我的Eclipse返回True
。但是,我尝试比较null
string对象,它抛出NullPointerException。
下面是代码。我还想知道为什么不同运行的代码会给出不同的结果。我将输出粘贴到下面
class Parent2 {
private String name;
private int id;
public static int count = 0;
public Parent2(String name, int id) {
this.name = name;
this.id = id;
if (this.getClass() == Parent2.class)
count++;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Parent2 other = (Parent2) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null) {
System.out.println("here2");
return false;
}
} else if (!name.equals(other.name)) {
System.out.println("here");
return false;
}
return true;
}
}
public class App {
public static void main(String[] args) {
Parent2 par = new Parent2("aa", 5);
Parent2 par2 = new Parent2(null, 5);
Parent2 par3 = new Parent2(null, 5);
System.out.println(par.equals(par2));
System.out.println(par2.equals(par));
System.out.println(par3.equals(par2));
System.out.println("----------------------------------------");
String str = null;
String str2 = null;
System.out.println(str.equals(str2));
System.out.println(str + "; " + str2);
}
}
每次执行的输出都不同,以下是一些:
here
false
here2Exception in thread "main" java.lang.NullPointerException
at RoundTwo.App.main(App.java:64)
false
true
----------------------------------------
输出2:
here
Exception in thread "main" java.lang.NullPointerException
at RoundTwo.App.main(App.java:64)
false
here2
false
true
----------------------------------------
输出3:
here
false
here2
false
true
Exception in thread "main" java.lang.NullPointerException
at RoundTwo.App.main(App.java:64)
----------------------------------------
输出4:
here
falseException in thread "main" java.lang.NullPointerException
at RoundTwo.App.main(App.java:64)
here2
false
true
----------------------------------------
输出5:
here
false
here2
false
true
----------------------------------------
Exception in thread "main" java.lang.NullPointerException
at RoundTwo.App.main(App.java:64)
只有最后一个输出执行的字符串等于抛出NullpointerException
感谢您的帮助:一般来说:如果控制流已经在实例方法中,则在其上调用该方法的实例不为空
因此在equals
方法中,两个对象都不能null
,因为控制流已经在实例方法中了。引用此
可以从不为空
。(在静态上下文中,它不可用,使用它会导致编译器错误)
但是,您眼前的问题是,如果要对其执行相等性检查的对象为null,则执行过程甚至不会输入您的equals
方法-您不能对null
对象调用实例方法:您会得到NullPointerException
其他涉及空值的案件
然而,比较器
实例应该处理这种情况:在这种情况下,它应该返回0
控制台输出的奇怪行为
控制台上消息的顺序不同,因为您写入两个不同的流:System.err
和System.out
。在这种情况下,不能保证打印消息的顺序与已写入的顺序相同。 < P>如果P1和P2均为空,则应返回<代码> Null PoExtExealOuts<代码>,因为不能在<代码> NULL/<代码>中调用等值方法。 < P> >在该方法中,不需要考虑此情况。如果此
为null
将自动执行NullPointerException
,并且无法覆盖此行为
方法重载时,将在o1上调用的确切方法。equals(o2)
取决于o1
的运行时类型。由于null
没有运行时类型(没有对象),因此没有可以调用的方法。因此,它将始终抛出NullPointerException
如果两个人的对象p1和p2为null,那么p1.equals(p2)应该返回什么
正如其他人所说,对null
变量执行任何操作都会引发NullPointerException
,这会导致NullPointerException。我想这是你问题的主要问题:
只有最后一个输出执行字符串,该字符串等于引发NullpointerException
这是错误的。您在每次执行时看到不同的输出是因为System.out.println()
写入System.out
PrintStream,而RuntimeException
s,如NullPointerException
,则写入System.err
PrintStream。两个流都在同一个控制台上结束写入,但不能保证它们刷新内容的时间。如果您想了解其行为,请使用单个缓冲区打印所有内容,包括异常,例如使用记录器而不是普通的System.out
调用。基本样本为:
public class App {
public static void main(String[] args) {
try {
Parent2 par = new Parent2("aa", 5);
Parent2 par2 = new Parent2(null, 5);
Parent2 par3 = new Parent2(null, 5);
System.out.println(par.equals(par2));
System.out.println(par2.equals(par));
System.out.println(par3.equals(par2));
System.out.println("----------------------------------------");
String str = null;
String str2 = null;
System.out.println(str.equals(str2));
System.out.println(str + "; " + str2);
} catch (Exception e) {
//this will do the "trick"
e.printStacktrace(System.out);
}
}
}
现在,每次执行应用程序时,您都会看到一个结果:
here
false
here2
false
true
----------------------------------------
Exception in thread "main" java.lang.NullPointerException
at RoundTwo.App.main(App.java:64)
然而,我尝试比较空字符串对象,它抛出NullPointerException
对p1
和p2
对象执行同样的操作,您将看到出现NullPointerException
。根据上述代码:
public class App {
public static void main(String[] args) {
try {
Parent2 par = new Parent2("aa", 5);
Parent2 par2 = new Parent2(null, 5);
Parent2 par3 = new Parent2(null, 5);
par = null;
par2 = null;
System.out.println(par.equals(par2)); // <-- NPE here
System.out.println(par2.equals(par));
System.out.println(par3.equals(par2));
System.out.println("----------------------------------------");
String str = null;
String str2 = null;
System.out.println(str.equals(str2));
System.out.println(str + "; " + str2);
} catch (Exception e) {
//this will do the "trick"
e.printStacktrace(System.out);
}
}
}
公共类应用程序{
公共静态void main(字符串[]args){
试一试{
PARTEN2 PAR=新PARTENT2(“AA”,5);
Parent2 par2=新Parent2(null,5);
Parent2 par3=新Parent2(null,5);
PAR=NULL;
par2=null;
系统输出打印LN(par等于(par2));//你永远不能在null
对象上调用方法;Java会在它到达你的代码之前抛出NullPointerException
。你不能在equals
方法中阻止它;你必须在调用它之前阻止它。啊,我现在明白了。谢谢你在代码中指出它。@user1988876欢迎。说这个
可以是空的
是正确的吗?但是它甚至在执行equal
方法之前就会抛出NPE
。所以这个
在某些情况下可以是空的,对吗?@user1988876不,这是不正确的。这个
变量不会是空的。