StringUtils.EMPTY vs StringUtils.isEmpty()
StringUtils.EMPTY is a final static member of StringUtil class that equates to "" i.e empty String.
whereas
StringUtils.isEmpty() Checks if a String is empty ("") or null.
How does StringUtils.EMPTY.equals(string) different from StringUtils.isEmpty(string)
public class ParentTest {
public static void main(String[] args){
String str = "";
String str2 = null;
if(StringUtils.EMPTY.equals(str)){
System.out.println("str1.1 is empty");
}
if(StringUtils.isEmpty(str)){
System.out.println("str1.2 is empty");
}
if(StringUtils.EMPTY.equals(str2)){
System.out.println("str2.1 is empty");
}
if(StringUtils.isEmpty(str2)){
System.out.println("str2.2 is empty");
}
}
}
results in
str1.1 is empty
str1.2 is empty
str2.2 is empty
Though StringUtils.isEmpty(str2) is saying that its checking for empty str2 but its actually checking both null as well as blank string for str2.
Comments
Post a Comment