Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Joel Elliott   on Oct 20 In Java Category.

  
Question Answered By: Troy Kelley   on Oct 20

In following link there's some utilities which can handle null, or the others ,
http://jakarta.apache.org/commons/lang/apidocs/index.html
for example:


public static Object defaultIfNull(Object object,
Object defaultValue)
Returns a default value if the object passed is null.
ObjectUtils.defaultIfNull(null, null) = null
ObjectUtils.defaultIfNull(null, "") = ""
ObjectUtils.defaultIfNull(null, "zz") = "zz"
ObjectUtils.defaultIfNull("abc", *) = "abc"

ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
Parameters:
object - the Object to test, may be null
defaultValue - the default value to return, may be null
Returns:
object if it is not null, defaultValue otherwise
or :

public static boolean isEmpty(String str)
Checks if a String is empty  ("") or null.
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
NOTE: This method changed in Lang version 2.0. It no longer trims the String. That functionality is available in isBlank().
Parameters:
str - the String to check, may be null
Returns:
true if the String is empty or null
or :

public static boolean isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false

Parameters:
str - the String to check, may be null
Returns:
true if the String is null, empty or whitespace
Since:
2.0

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 


Tagged: