When “” == s is false but “”.equals( s ) is true

前端 未结 10 1456
深忆病人
深忆病人 2020-11-30 19:46

EDIT Thanks for the prompt responses. Please see what the real question is. I have made it bold this time.

I do understand the difference between ==

相关标签:
10条回答
  • 2020-11-30 20:36

    If you use google code search, you can find lots of places where people make this same error: google for file:.java \=\=\ \"\" Of course, this can be a correct idiom in carefully controlled circumstances, but usually, its just a bug.

    0 讨论(0)
  • 2020-11-30 20:38

    Check this reference: http://mindprod.com/jgloss/string.html#COMPARISON at the excellent Canadian Mind Products Java & Internet Glossary. Worth a bookmark.

    0 讨论(0)
  • 2020-11-30 20:41

    Would you expect "abcde".substring(1,2) and "zbcdefgh".substring(1,2) to yield the same String object?

    They both yield "equal" sub-strings extracted from two different Strings, but it seems quite reasonable that tehy are different objects, so == sees them as different.

    Now consider when the substring has length 0, substring(1, 1). It yields a zero length String, but it's not surprising that the "abcde".substring(1,1) is a different object from "zbcdefgh".substring(1,2) and hence at least one of them is a different object from "".

    0 讨论(0)
  • 2020-11-30 20:50

    You should try considering String.length() == 0.

    0 讨论(0)
提交回复
热议问题