String comparison without worrying about NullPointerException

How to avoid NulPointerException when performing string comparison of String in Java

python


String s = null;
if("hello".equals(s)) {
    System.out.println("Hello!");
}

Consideration by all means

NullPonterException is squeezed as shown in "** When judging a character string by equals **" There is also an opinion that it is not good.

Personally, I've written code so far, and I've never had to handle it as an exception when s = null, so I think this is a good way to write it. ~~ (Although exception handling is sparse because I am a student and have no practical experience)

Postscript

We received various comments and reminded us of the importance of null checking. When I compare strings with String in the future, I will do something like s.equals ("hello") and do a null check.

Finally, we would like to thank everyone for their valuable comments.

Recommended Posts

String comparison without worrying about NullPointerException
MyBatis string comparison
[Java] String comparison and && and ||
About Java String class
About size comparison of compareTo
[Java] About String and StringBuilder
Explanation about Ruby String object