访谈:Java Equals [英] Interview : Java Equals
本文介绍了访谈:Java Equals的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在采访中被问到这个问题。以下哪项更好用
I was asked this question in interview. Which of the following is better to use
MyInput.equals("Something");
或
"Something".equals(MyInput);
谢谢
推荐答案
我会选择
"Something".equals(MyInput);
在这种情况下,如果MyInput是 null
那么它不会抛出 NullPointerException
in this case if MyInput is null
then it won't throw NullPointerException
这里我们确定等于的对象()
将要调用 NOT NULL
。
Here we are sure that the object on which equals()
is going to invoke is NOT NULL
.
如果您希望代码中的 NullPointerException
做出一些决定或抛出/包装它,那就先去吧。
And if you expect NullPointerException
from your code to take some decision or throw/wrap it, then go for first.
没有性能影响
这篇关于访谈:Java Equals的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文