Innovative way for checking if a String is null in java

Checking for null in java can be interesting. I will talk about checking null with String.

There are two innovative ways to do this, without having to check for a specific null check.

First :

 

String n = null;
  if("navnith".equalsIgnoreCase(n))
   System.out.println(" got it here ");

and this way

  if(n instanceof String && n.trim().equalsIgnoreCase("navnith") )
 

Not a lot of people know but the instanceof operator will also check for null.

Enjoy.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: