问题
I want to check for null or empty specifically in my code. Does empty and null are same for StringBuilder
in Java?
For example:
StringBuilder state = new StringBuilder();
StringBuilder err= new StringBuilder();
success = executeCommand(cmd, state, err);
/* here executeCommand() returns empty or null in state, I cant make changes in <br/> executeCommand() so can I check it in my code somehow for state, if its null or empty? */<br/>
if (state == null) { //do blabla1 }
if (state.tostring().equals("")) { //do blabla2 }
Does above code make sense or how should I change it?
回答1:
No, null
and empty
are different for StringBuilder
.
StringBuilder nullBuilder = null;
if(nullBuilder == null) {
System.out.println("Builder is null");
}
&
StringBuilder emptyBuilder = new StringBuilder("");
if(emptyBuilder == null || emptyBuilder.toString().equals("")) {
System.out.println("Builder is empty");
}
回答2:
In Java, null
is a reference literal. If a variable is null
then is not referring to anything.
So, if you have StringBuilder s = null
, that means that s
is of type StringBuilder
but it is not referring to a StringBuilder
instance.
If you have a non-null
reference then you are free to call methods on the referred object. In the StringBuilder
class, one such method is length()
. In fact if you were to call length()
using a null
reference then the Java runtime will throw a NullPointerException
.
Hence, this code is quite common:
If (s == null || s.length() == 0/*empty if the length is zero*/){
// do something
It relies on the fact that evaluation of ||
is from left to right and stops once it reaches the first true
condition.
回答3:
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
回答4:
No. empty
means, that there are no characters in the StringBuilder. null
means that there is no StringBuilder object at all.
A variable is only null
if it has a reference type (for example String, StringBuilder, Set, as a thumbrule: all capitalized types) and it is not initialised yet or has been set explicitly to null
.
回答5:
The below code may help you,
StringBuffer sb = new StringBuffer();
String str = sb.toString();
if(!"".equals(str)) {
System.out.println("String : " + str);
} else {
System.out.println("Empty Builder");
}
回答6:
You can try like this
StringBuilder state = new StringBuilder();
if(StringUtils.isNotBlank(state .toString())){
//this will check for null, " ", ""
}
来源:https://stackoverflow.com/questions/21553562/how-to-check-null-on-stringbuilder