Groovy ==运算符 [英] Groovy == operator

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

问题描述

到现在为止,我的理解是 == .equals()的操作符重载。不过,我最近发现:

  new Integer(1)== new Long(1)//返回true 



  new Integer(1).equals(new Long(1))//返回false 

== 并不完全是 .equals()的缩写,所以它如何确定相等性?

在Groovy中,

解决方案 c 大致相当于 equals ),然而,如果类是 Comparable ,那么在比较具有相同值的不同类时,会发现它与Java不同。如果可能,Groovy也会进行类型转换。



如果您查看代码,它最终看起来像 compareToWithEqualityCheck() 被执行为 ==


Until now, my understanding was that == is an operator overload for .equals(). However, I recently discovered that

new Integer(1) == new Long(1) // returns true

whereas

new Integer(1).equals(new Long(1)) // returns false

so I guess == is not exactly a shorthand for .equals(), so how does it determine equality?

解决方案

== in Groovy is roughly equivalent to equals(), however, you'll find it's different from Java when comparing different classes with the same value - if the class is Comparable. Groovy also does type casting if possible.

If you check out the code, it looks like ultimately compareToWithEqualityCheck() is executed for ==.

这篇关于Groovy ==运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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