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

前端 未结 7 1321
故里飘歌
故里飘歌 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:22

    That saves you from a NullPointerException.

    That is Yoda Condition used to solve unsafe null behavior.

    In programming jargon, Yoda Conditions (also called Yoda Notation) is a programming style where the two parts of an expression are reversed in a conditional statement.

    Advantage is

    Swapping the two conditional values does not change the behavior of the program. A common mistake is to accidentally assign a value instead of writing a conditional statement.

提交回复
热议问题