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

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

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

Is there any reason to write such a string comparison ?.

This might be some kind of best / bad practice or some kind of code that might bring some benefit. I would like to know if I can use this in commercial projects.

+4
source share
7 answers

This will save you from a NullPointerException.

This Yoda State is used to address unsafe null behavior.

In software jargon, Yoda conditions (also called Yoda notations) are a programming style in which two parts of an expression are referenced in a conditional expression.

Advantage

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

+9
source

It was called the Yoda condition and was used to avoid a NullPointerException . As for its use in commercial projects, this is really a design decision - some developers want to be protected from zero values, while others want a quick failure mechanism offered by a regular recording.

+2
source

If you have a code:

 if( "NODENAME".equals(xmlreader.getNodeName()) ){...} 

It will avoid a NullPointerException when xmlreader.getNodeName() is null since

 "NODENAME".equals(null) 

will return false instead of a NullPointerException .

PS: Keep in mind that if for some reason the xmlreader itself is null, then:

 "NODENAME".equals(xmlreader.getNodeName()) 

may still throw a NullPointerException .

+2
source

Usually the string comparison was written so as to avoid a NullPointerException if xmlreader.getNodeName() is null, because then you will have

 if("NODENAME".equals(null)) { // ... } 

compared with

 if(null.equals("NODENAME")) { // ... } 

which would be cast.

This is called the Yoda condition:

enter image description here

if you expect xmlreader.getNodeName() be null , then this will be fine, otherwise I would not rely on this to avoid throwing an exception, you should rather handle it earlier in your code.

+2
source

The equals() method checks the contents of a String . calling the equals() method in a literal saves you from a NullPointerException , which can occur if xmlReader.getNodeName() returns null . Calling the equals method on null will NullPointerException

0
source

If you get the impression that you can also compare strings using == , then it will fail, even if two "significant" equal String are involved in the comparison, since == checks to see if it is the same object. Of course, as other answers claim, you avoid a NullPointeException if you call equals on a standard string, as in your question.

0
source

Since equals is a built-in method in java, we need to make sure that it must be called with an object that does not have zero. so in case we call with null, we end up with a null pointer exception.

Example: suppose I need to check if the string a is "hai" and a is equal to the user.

then I'm not sure if "a" will be null or not. therefore, if I use a.equals("hai") , then it is unsafe if a becomes null, but if you change the comparison, then it is always safe, whether it is null or not.

therefore always prefer "hai".equals(a) be safe from null pointer exceptions.

0
source

Source: https://habr.com/ru/post/1500488/


All Articles