What are the benefits of “String”.equals(otherString)

前端 未结 7 1311
故里飘歌
故里飘歌 2021-01-19 04:57

in a tutorial (for implementing a xml parser) i saw the following code:

if( \"NODENAME\".equals(xmlreader.getNodeName()) ) {  // getNodeName() returns java.l         


        
相关标签:
7条回答
  • 2021-01-19 05:29

    If you have the impression that you could also compare the Strings using == then it would fail even if in the comparison would be involved two "meaningfully" equal String, since == checks if it is the same Object. Of course, as the other answers state, you avoid a NullPointeException if you call equals on a standard String, as in your question.

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