Sjk Sjk -4 years ago 83
Java Question

Checking if condition for 'null'

I have a doubt regarding checking null condition.For eg :

if(some conditon)
value1= value; //value1 is string type
value1= "";

Similarly some 4 other string value has similar condition.
What i need is i want to check whether all those 5 string value is null or not,Inorder to do some other specific part.
i did it like this

if(value1 == null)

but the pgm control didnot entered the loop eventhough value1="".
then i tried

if(value1 ==""){

this also didnt worked.

Cant we check null and "" value as same??
can anyone help me??

Answer Source

If you want to check is a String is null, you use

if (s == null)

If you want to check if a string is the empty string, you use

if (s.equals(""))


if (s.length() == 0)


if (s.isEmpty())

An empty string is an empty string. It's not null. And == must never be used to compare string contents. == tests if two variables refere to the same object instance. Not if they contain the same characters.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download