• Post Reply Bookmark Topic Watch Topic
  • New Topic

to check a string if null  RSS feed

 
Gp Anand
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My method, findIP tried to find an entry from hashmap and returns value if it finds.  However, if input value not found, the String serverName returns null.  I wanted to check this condition and return meaningful value. 
But my if condition looks like not working here.  Can you please help.  I tried with different combination, and none of them work.   like - 
if (serverName.equals(""))
if (serverName.isEmpty())





Thanks,
-Anand

 
Norman Radder
Ranch Hand
Posts: 146
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try variable == null
 
Gp Anand
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ok   Thanks Norman, it worked

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The contract for equals() states:
Java API Documentation for Object.equals() wrote:...
For any non-null reference value x, x.equals(null) should return false

A valid call to equals(null) will always return false.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A valid call to equals(null) will always return false.

Does that say the contents of an object can not be null?
The contents of the variable that refers to the object can't be null, otherwise the statement would throw a NPE.

It seems there are two values to be considered:
the contents of the reference variable
and the contents of the object.

the equals() method looks at the contents of the object
the == operator looks at the reference variable
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norman Radder wrote:. . . Does that say the contents of an object can not be null?  . . .
No, it means that an object which exists is implicitly different from an object which doesn't exist. Apart from my confused philosophy, that is.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By "a valid call" I mean that you're calling .equals() on a non-null reference. If you try to call .equals() on a null reference, you're going to get a NullPointerException.

So, given a valid object reference, calling equals(null) on that object should always return false, no matter what the contents of the object. The reasoning here is that nothing is equals() to null. So, to check if a reference is null, you have to use the reference equality operator, ==.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Norman Radder wrote:. . . Does that say the contents of an object can not be null?  . . .
No, it means that an object which exists is implicitly different from an object which doesn't exist.

That's a great way to put it, Campbell. 
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good grief! Has somebody given me pie? I hope that is pie which does exist and not just philosophical pie, and thank you whoever it was
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a piece of existential pie. Enjoy. And you're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!