Hi and good day to all,

Just a curious question, when I am comparing a String object with blank space(" "), I type x.equals(" ") in my Netbeans IDE(x is a String type variable), but Netbeans automatically shows it's an error and change it to " ".equals(x) instead.

Anyone here know about it and keen to answer my question??
Thank you.

Recommended Answers

that would be the most logical way.
it's not really an error, rather a suggestion:

if ( x.equals(" "))
NetBeans detects the possibility to this code being run without x being initialized. that would lead to a NullPointerException.

if ( " ".equals(x))
here you are calling the equals method on …

Jump to Post

All 2 Replies

that would be the most logical way.
it's not really an error, rather a suggestion:

if ( x.equals(" "))
NetBeans detects the possibility to this code being run without x being initialized. that would lead to a NullPointerException.

if ( " ".equals(x))
here you are calling the equals method on the " " instance of String, it's impossible for it not to be instantiated, hence it is impossible for a NullPointerException to be thrown.

if you add the ! in front, like this:
if ( !" ".equals(x))
it just means: 'IF NOT " " EQUALS X'

stultuske, thank you for your answer. Now I'm more clear about that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.