I want to check for null or empty specifically in my code. Does empty and null are same for StringBuilder
in Java?
For example:
StringBuilde
Null mean, there are no object in the heap for that reference variable. This is common to all java object, not specific to StringBuilder
and Empty means, ""
.
In your code, you have created a StringBuilder object, so checking null is redundant. And, You can check empty by using isEmpty()
method in from java String api
if(state.tostring().isEmpty()) {
//
}
And checking null is correct. Find the corrected version here
if (state == null) {
// ...bla 1
} else if (state.tostring().isEmpty()) {
//... bla 2
}
Your second if
condition will throw NullPointerException
, if the state
is null. So if
should be nested with if else