Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Caveat: This will yield different results whether you initialize the string as

> String a = "foo";

vs.

> String a = new String("foo");



Parent wrote:

"Any two identical literals"

The second 'a' is not a literal.


The OP is still correct though - don't use == for strings, always use .equals()

The problem with using == is maintainability and silent failure. Someone may change the program to accept the string from the command line, or just about anything else. As soon as this happens, a string can come in from elsewhere and still have the value "yes" but would still pass the inequality.

This bug would be completely silent and incredibly difficult to debug, possibly only arising in strange and unusual circumstances. The only way to really avoid this is to just use .equals() always. Using == for strings in Java is plain terrible coding, and the OP is therefore correct.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: