Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a string is not null?

Tags:

java

if(string.equals(""))
{

}

How to check if the string is not null?

if(!string.equals(""))
{

}
like image 746
sandeep Avatar asked Mar 31 '11 12:03

sandeep


People also ask

How do you check a string is null or not?

You can use the IsNullOrWhiteSpace method to test whether a string is null , its value is String. Empty, or it consists only of white-space characters.

What is != null in Java?

The java. lang. NullPointerException is thrown in Java when you point to an object with a null value. Java programmers usually encounter this infamous pointer exception when they forget to initialize a variable (because null is the default value for uninitialized reference variables).

Will isEmpty check for null?

isEmpty(<string>) Checks if the <string> value is an empty string containing no characters or whitespace. Returns true if the string is null or empty.


2 Answers

Checking for null is done via if (string != null)

If you want to check if its null or empty - you'd need if (string != null && !string.isEmpty())

I prefer to use commons-lang StringUtils.isNotEmpty(..)

like image 136
Bozho Avatar answered Nov 15 '22 09:11

Bozho


You can do it with the following code:

 if (string != null) {

 }
like image 30
RoflcoptrException Avatar answered Nov 15 '22 09:11

RoflcoptrException