用==比较Java字符串 [英] Comparing java Strings with ==

查看:134
本文介绍了用==比较Java字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆