18

I have a setter method.

Then when another (say generate) method is run, I need to check the value of my fields. So in the case of String property, I need to know if it contains the value or if it was not set. So it may be null, "" or something meaningful, there are 3 possibilities. And it is rather boring to check first for a null value :

if (s != null)

then for an empty String

if (!s.isEmpty())

is there a one-step check here? You can tell me that I can initialize my String field with an empty String. [ IS IT COMMON? ] But what if someone passes a null value to the setter method setS? so do we always have to check if the Object value is null or not before doing something with that object?

Well, yes a setter method can check it's values and also a getter method can return a non-null value if the field is null. But is it the only solution? It 's too much work in getters & setters for a programmer to do!

4
  • 1
    Yes Apache library. Why, again, it is not in standart java distribution? Commented Dec 16, 2009 at 9:05
  • 2
    something to think about : null means not set, "" means set to empty. Commented Dec 16, 2009 at 9:46
  • Agree with Pat here. It sometimes seems like this is just unnecessary book keeping but they are semantically different. If the member is null, it means "has no value" whereas an empty string "has a value; an empty string". It is common for methods to have different behavior (by design) based on this fact. Commented Dec 16, 2009 at 10:39
  • Sorry to spam the comments, but since it's somewhat related, you can avoid the check-for-null-and-compare test on non-empty strings by having the string-literal do the test (string-literals are still Strings), e.g. "CheesySafeCompare".equals( someString ) works as you'd imagine but will not throw a NullPointerException. Commented Dec 16, 2009 at 10:48

8 Answers 8

18

Commons library, StringUtils.isBlank() or StringUtils.isEmtpy().

isEmpty is equivalent to

s == null || s.length() == 0

isBlank is equivalent to

s == null || s.trim().length() == 0
Sign up to request clarification or add additional context in comments.

1 Comment

little remark, what you described are isNotBlank & isNotEmpty :)
5
if(s != null && !s.isEmpty()){
// this will work even if 's' is NULL
}

1 Comment

Although this prevents a null exception, this is not an equivalent test: the expression resolves to true if s is null. This is a null-safe way to test whether s contains an empty string, it does not test whether s is empty and not null. The difference is important!
4

In the Jakarta Commons there is a StringUtils.isEmpty(String).

Comments

1

use org.apache.commons.lang.StringUtils, the method StringUtils.isNotBlank check both nullity and emptiness.

Comments

0

Add maven dependency for com.google.guava

Then in your code:

import com.google.common.base.Strings;

if(!Strings.isNullOrEmpty(s)) {
  // Do stuff here
}

Comments

0

If you are doing android development, you can use:

TextUtils.isEmpty (CharSequence str) 

Added in API level 1 Returns true if the string is null or 0-length.

Comments

0

Spring

Spring provides the class org.springframework.util.StringUtils which includes these relevant methods.

hasText(String s)

Returns true if and only if a String or CharSequence contains a character which is not a space.

Example:

 StringUtils.hasText(null) = false
 StringUtils.hasText("") = false
 StringUtils.hasText(" ") = false
 StringUtils.hasText("12345") = true
 StringUtils.hasText(" 12345 ") = true

hasLength(String s)

Returns true if and only if a String or CharSequence contains any characters, spaces included.

Example:

 StringUtils.hasLength(null) = false
 StringUtils.hasLength("") = false
 StringUtils.hasLength(" ") = true
 StringUtils.hasLength("Hello") = true

In the case of the question, hasText would be the best option.

Comments

0

its not String. it should be Strings. as an example:

if(!Strings.isNullOrEmpty()

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.