琴弦为什么不比较的参考? [英] Why strings does not compare references?

查看:127
本文介绍了琴弦为什么不比较的参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是特殊情况,但为什么字符串之间==如果它们的值等于,而不是当他们的参考值等于返回。它有事情做与overlloading操作?

I know it is special case but why == between strings returns if their value equals and not when their reference equals. Does it have something to do with overlloading operators?

推荐答案

== 运营商在字符串超载进行平等的价值,而不是引用相等,确实如此。这样做是为了使弦更友好的程序员,以避免使用时引用相等比较他们(而不是在Java中太常见,特别适合初学者)中出现的错误。

The == operator is overloaded in String to perform value equality instead of reference equality, indeed. The idea is to make strings more friendly to the programmer and to avoid errors that arise when using reference equality to compare them (not too uncommon in Java, especially for beginners).

到目前为止,我从来没有需要参照比较字符串,是诚实的。如果你需要做的,你可以使用 object.ReferenceEquals()

So far I have never needed to compare strings by reference, to be honest. If you need to do it you can use object.ReferenceEquals().

这篇关于琴弦为什么不比较的参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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