What are the benefits of "String".equals(otherString)

some_coder

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

if( "NODENAME".equals(xmlreader.getNodeName()) ) {  // getNodeName() returns java.lang.String
... 
}

Is there a reason to write a string comparision like that?.

This may be some kind of best/bad practice or code like that could give some performance benefit. I would like to know if i should use this in commercial projects.

Suresh Atta

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.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

What is the benefits of modules in google app engine

분류에서Dev

What benefits could the average geek get out of Powershell?

분류에서Dev

What are benefits of using Aspectj in Spring config, if aspectj can be used alone

분류에서Dev

What are the benefits of compiling with -j1 instead of -jN

분류에서Dev

What are the benefits of using a 64-bit os, vs a 32-bit one, on the same hardware ?

분류에서Dev

Should website testing include passing W3C css & html validator?What are the benefits of passing them?

분류에서Dev

Swift: Benefits of Curry Function

분류에서Dev

Benefits of Separating Home Directory?

분류에서Dev

Benefits of Separating Home Directory?

분류에서Dev

Benefits of Static Functions for Class Constructions

분류에서Dev

What is the most efficient bway to get len of string ?

분류에서Dev

What is the spatial complexity of Map<Integer, List<String>>?

분류에서Dev

What does really mean null string?

분류에서Dev

What is the best ASCII char to separate a string?

분류에서Dev

What does %[^\n] mean in a scanf() format string

분류에서Dev

What's wrong with this string equality comparison? (Python)

분류에서Dev

What is wrong in this string concatenation using JavaScript?

분류에서Dev

What is the purpose of an ampersand (&) in a PyQt4 string?

분류에서Dev

What is the difference between single quoted $'string' and double quoted $"string" in bash?

분류에서Dev

In C, what exactly does something like "a string"[4] mean and signify?

분류에서Dev

What is the meaning of the tilde in some debian openjdk package version string?

분류에서Dev

What's the meaning of plain string when using as condition?

분류에서Dev

What is the easiest way to find out character repetition in a string using JS?

분류에서Dev

Given a String, What is the Length of the One of the Longest WFF in Polish Notation?

분류에서Dev

What's the best way to transform a string into an associative array

분류에서Dev

what can I use sed to search for a string and append /sub1 and some file ../ before search string

분류에서Dev

Do I need to escape every string and what is the maximum length of an escaped string?

분류에서Dev

Benefits of using Squid proxy in modern browsers (Chrome, Firefox, Safari)

분류에서Dev

27 inch Monitor with USB Type C connector, benefits?

Related 관련 기사

  1. 1

    What is the benefits of modules in google app engine

  2. 2

    What benefits could the average geek get out of Powershell?

  3. 3

    What are benefits of using Aspectj in Spring config, if aspectj can be used alone

  4. 4

    What are the benefits of compiling with -j1 instead of -jN

  5. 5

    What are the benefits of using a 64-bit os, vs a 32-bit one, on the same hardware ?

  6. 6

    Should website testing include passing W3C css & html validator?What are the benefits of passing them?

  7. 7

    Swift: Benefits of Curry Function

  8. 8

    Benefits of Separating Home Directory?

  9. 9

    Benefits of Separating Home Directory?

  10. 10

    Benefits of Static Functions for Class Constructions

  11. 11

    What is the most efficient bway to get len of string ?

  12. 12

    What is the spatial complexity of Map<Integer, List<String>>?

  13. 13

    What does really mean null string?

  14. 14

    What is the best ASCII char to separate a string?

  15. 15

    What does %[^\n] mean in a scanf() format string

  16. 16

    What's wrong with this string equality comparison? (Python)

  17. 17

    What is wrong in this string concatenation using JavaScript?

  18. 18

    What is the purpose of an ampersand (&) in a PyQt4 string?

  19. 19

    What is the difference between single quoted $'string' and double quoted $"string" in bash?

  20. 20

    In C, what exactly does something like "a string"[4] mean and signify?

  21. 21

    What is the meaning of the tilde in some debian openjdk package version string?

  22. 22

    What's the meaning of plain string when using as condition?

  23. 23

    What is the easiest way to find out character repetition in a string using JS?

  24. 24

    Given a String, What is the Length of the One of the Longest WFF in Polish Notation?

  25. 25

    What's the best way to transform a string into an associative array

  26. 26

    what can I use sed to search for a string and append /sub1 and some file ../ before search string

  27. 27

    Do I need to escape every string and what is the maximum length of an escaped string?

  28. 28

    Benefits of using Squid proxy in modern browsers (Chrome, Firefox, Safari)

  29. 29

    27 inch Monitor with USB Type C connector, benefits?

뜨겁다태그

보관