== 和 eq 运算符 [英] == and eq Operators

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

问题描述

scala> val x = "a"
x: String = a

scala> val y = "a"
y: String = a

据我所知,== 会调用 equals(值相等).

As I understand, == will call equals (value equality).

scala> x == y
res18: Boolean = true

但是,x eq y,出乎我的意料,显示为真.据我了解,eq 检查对象身份.

But, x eq y, unexpected to me, shows true. eq, as I understand, checks for object identity.

scala> x eq y
res19: Boolean = true

Scala 编译器是否足够智能以返回相同的(身份)String 对象?我相信这个词是实习.

Is the Scala compiler smart enough to return the same (identity) String object? I believe the term is interning.

或者,eq 是否真的在执行值相等?

Or, is eq actually performing a value equality?

推荐答案

Scala 的 String 实际上是 Java.Lang.String,实际上使用实习 - 参见 Scala 参考 -

Scala's String is actually Java.Lang.String, which in fact uses interning - see Scala Reference -

type String        = java.lang.String

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

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