Why strings does not compare references?

后端 未结 5 1083
耶瑟儿~
耶瑟儿~ 2021-01-11 23:54

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 operat

5条回答
  •  执笔经年
    2021-01-12 00:07

    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).

    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().

提交回复
热议问题