用==比较Java字符串 [英] Comparing java Strings with ==
问题描述
可能重复:
Java String.equals与==
Possible Duplicate:
Java String.equals versus ==
是否可以使用==运算符比较Java字符串?
Is it possible to compare Java Strings using == operator?
为什么我经常看到使用equals()方法代替?
Why do I often see, that equals() method is used instead?
是因为在使用==与文字字符串(例如"Hello")进行比较时,并不意味着调用equals()吗?
Is it because when comparing with literal Strings (like "Hello") using == doesn't imply calling equals()?
推荐答案
在Java中没有 custom 运算符重载. [所以您不能重载它来调用equals()]
there is no custom operator overloading in java. [so you cannot overload it to call equals()]
equals()确保您检查2个对象是否相同,而==检查这是否是完全相同的对象. [因此,不使用==不会调用equals()].
the equals() ensures you check if 2 Objects are identical,while == checks if this is the exact same object. [so no, using == does not invoke equals()].
这篇关于用==比较Java字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!