2

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

if(some conditon)
value1= value;  //value1 is string type
else 
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??

3
  • 2
    What do you mean by didn't work? What was the expected output? and what output did you got? Please explain your use case more clearly. Commented Feb 9, 2013 at 11:01
  • "" is not null Commented Feb 9, 2013 at 11:01
  • @JBNizet didnt worked means Even the value for value1 is "" then also control didnt entered that loop.I checked by debugging Commented Feb 9, 2013 at 11:04

6 Answers 6

14

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(""))

or

if (s.length() == 0)

or

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.

Sign up to request clarification or add additional context in comments.

9 Comments

thnkz it worked.Also 1 more dbt among last 3 methods which one is more effective.Means whether those 3 have same impact ?
AFAIK,among the last 3 methods,middle one contains little overhead task to calculate the exact lengh before comparing it with 0.and that isn't your goal,your goal is to just check whether it's empty or not.and sometimes it may happen that your string is very large.so why to do overhead of calculating length.so,the first or the third should be the right choice.
Yes, of course it returns the length of the string. But it doesn't need to count chars. The length is precomputed and stored into a field of the String class. There's nothing to calculate. Look at the source code of String.length().
You never change a string, since String is immutable. Every "mutating" operation (like concat(), substring(), etc.) returns a new String object and leaves the original string unmodified.
When you do that, you create 3 different String instances, each with their count field initialized when the instance is constructed.
|
5

To check both "is not null" and "is not empty" on a String, use the static

TextUtils.isEmpty(stringVariableToTest)

Comments

3

It looks like you want to check wether a String is empty or not.

if (string.isEmpty())

You can't check that by doing if (string == "") because you are comparing the String objects. They are never the same, because you have two different objects. To compare strings, use string.equals().

Comments

1

When you are working on String always use .equals.

equals() function is a method of Object class which should be overridden by programmer.

If you want to check the string is null then if (string.isEmpty()) else you can also try if (string.equals(null))

1 Comment

This answer is wrong. string.equals(null) will throw a NullPointerException if the variable string is in fact null, as would string.isEmpty(), or any method called on a variable that is null.
1

You can use:

we can check if a string is empty in 2 ways:

  • if(s != null && s.length() == 0)
  • if(("").equals(s))

2 Comments

You can simply use TextUtils.isEmpty() instead of the first way.
@Sean. Provided OP is allowed to use a 3rd party library. I would certainly not prefer any 3rd party library for such a minor task.
0

prefer below.

String str;
if(str.length() > 0)
{
     Log.d("log","str is not empty");
}
else
{
     Log.d("log","str is empty");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.