Java 中 == 的奇怪行为
我在 java 中观察到一个奇怪的行为 ==
运算符。我正在尝试打印输出,如下所示
String str1 = "Rajesh";
String str2 = "Rajesh";
System.out.println("Using equals() str1 and str2 Equals :"
+ str1.equals(str2));
System.out.println("Using == str1 and str2 Equals :"
+ str1 == str2);
第一个 SOP 语句打印
使用 equals() str1 和 str2 等于 :true
并且下一个 SOP 仅打印 false 。
我尝试在 Eclipse 和 Net Beans 中进行编译,但结果是相同的。 我很困惑为什么
使用 == str1 和 str2 等于:
未打印
帮我解决这个问题
提前致谢,
Raj
I observed a strange behavior ==
operator in java. I am trying to print the out put as follows
String str1 = "Rajesh";
String str2 = "Rajesh";
System.out.println("Using equals() str1 and str2 Equals :"
+ str1.equals(str2));
System.out.println("Using == str1 and str2 Equals :"
+ str1 == str2);
The first SOP statement printing
Using equals() str1 and str2 Equals :true
and the next SOP printing only false .
I tried compiling in both eclipse and Net Beans but result is the same .
I am so confused why
Using == str1 and str2 Equals :
is not printing
Help me out in this
Thanks in advance,
Raj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
它与
("Using == str1 and str2 Equals :" + str1) == str2
相同,当然这是错误的。表达式从左到右解析,因此首先连接"Using == str1 and str2 Equals :"
和str1
,然后应用==
运算符。it's the same as
("Using == str1 and str2 Equals :" + str1) == str2
and this is false, of course. Expression is parsed from left to right and so at first it concatenates"Using == str1 and str2 Equals :"
andstr1
, then applies==
operator.有关运算符优先级表,请参阅 http://bmanolov.free.fr/javaoperators.php爪哇。
+ 运算符的优先级高于 == 运算符。
因此,实际上,您的代码等效于以下内容:
请注意我添加的括号的位置。它的计算结果如下:
然后是这样:
然后是这样:
为了获得您想要的结果,您必须使用括号来指定您希望在 + 运算符之前解析 == 运算符:
注意新的放置位置括号。
See http://bmanolov.free.fr/javaoperators.php for a table of operator precedence in Java.
The + operator is higher precedence than the == operator.
So, in effect, your code is equivalent to the following:
Note the placement of the parentheses that I added. It evaluates to this:
And then to this:
And then to this:
In order to get the result you want, you must use parentheses to specify that you want the == operator to be resolved BEFORE the + operator:
Notice the new placement of the parentheses.
因为 + 与 = 相比具有更高的优先级,并且如果您使用括号(str1 == str2) 那么
这个结果为 true,因为最高优先级是 (.
所以首先它检查括号内的数据。
输出:
Because + has higher priority compare to = and if you use bracket(str1 == str2) then
this result give true because highest priority is (.
So First it checks bracket inside data.
Output:
也许是操作顺序的问题?尝试:
Maybe an order of operations thing? Try:
尝试用 () 包围它,如下所示:
Try surrounding it with () like this:
equals 方法当且仅当 x 和 y 引用同一个对象时才返回 true。Follwoing 是 equals 方法的 Object 类实现。
在 String 类中,此方法已被重写,如下所示。
如果您使用 == 运算符,它只需检查两个引用是否具有相同的对象。类似于Object类的equals方法。
equals method returns true if and only if x and y refer to the same object.Follwoing is the Object class implementation of equals method.
In String class this method has overridden as following.
And if you use == operator it just check both references are having same object. Similar to the Object class equals method.
原因是 Java 中不能使用
==
来比较字符串。在 C++ 或 C#(或支持运算符重定义的其他语言)中,您可以覆盖
==
运算符以提供该功能。 Java 不支持这一点。The reason is that you cannot compare strings in Java using
==
.In C++ or C# (or other languages supporting operator redefinition), you can overwrite the
==
operator to provide that functionality. Java does not support that.str1.equals(str2)
返回true
因为equals()
函数比较字符串变量的内容,其中==
运算符比较实例。由于str1
和str2
是String
类实例的两个差异,因此它返回false
str1.equals(str2)
returnstrue
because theequals()
function compares the content of the string variables, where as==
operator compares the instances. Sincestr1
andstr2
are two differences of instances ofString
class, it returnsfalse
在Java中,
==
运算符匹配两个对象,即它们的地址,而.equals()
方法对两个对象的值进行数学计算,这就是为什么你得到true 代表
equals()
,false
代表==
,因为两者是不同的对象。In Java
==
operator matches the two objects i.e their address while.equals()
method mathces the values of both objects, thats why you are gettingtrue
forequals()
andfalse
for==
as both are different objects.==
只能用于比较原始数据类型。要比较对象,您需要使用equals
方法。在对象上使用==
运算符实际上是比较地址而不是值。==
can only be used to compare primitive datatypes. To compare objects you need to useequals
method. Using a==
operator on objects actually compares there addresses instead of values.