Java: Why can String equality be proven with ==?

后端 未结 3 1216
野的像风
野的像风 2020-12-06 17:46

I learned that it is from the devil to test String equality with == instead of String.equals(), because every String was a reference to its own obj

3条回答
  •  醉梦人生
    2020-12-06 18:26

    It hasn't changed. However, the Java Compiler uses string.intern() to make sure that identical strings in source code compile to same String object. If however you load a String from a File or Database it will not be the same object, unless you force this using String.intern() or some other method.

    It is a bad idea, and you should still use .equals()

提交回复
热议问题