Java 如何在 StringBuilder 上检查 null?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21553562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check null on StringBuilder?
提问by user1145280
I want to check for null or empty specifically in my code. Does empty and null are same for StringBuilder
in Java?
我想在我的代码中专门检查 null 或 empty 。StringBuilder
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?
上面的代码有意义还是我应该如何更改它?
采纳答案by Kick
No, null
and empty
are different for StringBuilder
.
不,null
并且empty
对于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");
}
回答by markusthoemmes
No. empty
means, that there are no characters in the StringBuilder. null
means that there is no StringBuilder object at all.
No.empty
表示 StringBuilder 中没有字符。null
意味着根本没有 StringBuilder 对象。
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
.
变量仅null
当它具有引用类型(例如 String、StringBuilder、Set,作为拇指规则:所有大写类型)并且尚未初始化或已显式设置为null
.
回答by Abimaran Kugathasan
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, ""
.
空意味着,该引用变量的堆中没有对象。这对所有java对象都是通用的,而不是特定于StringBuilder
和Empty的意思,""
.
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
在您的代码中,您创建了一个 StringBuilder 对象,因此检查 null 是多余的。并且,您可以使用isEmpty()
java String api 中的方法检查空
if(state.tostring().isEmpty()) {
//
}
And checking null is correct. Find the corrected version here
并且检查 null 是正确的。在这里找到更正的版本
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
如果为空,您的第二个if
条件将抛出。所以应该嵌套NullPointerException
state
if
if else
回答by Bathsheba
In Java, null
is a reference literal. If a variable is null
then is not referring to anything.
在 Java 中,null
是引用文字。如果一个变量是null
那么不是指任何东西。
So, if you have StringBuilder s = null
, that means that s
is of type StringBuilder
but it is not referring to a StringBuilder
instance.
所以,如果你有StringBuilder s = null
,那意味着它s
是类型的,StringBuilder
但它不是指一个StringBuilder
实例。
If you have a non-null
reference then you are free to call methodson 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
.
如果您有一个非null
引用,那么您可以自由调用引用对象上的方法。在StringBuilder
类中,一种这样的方法是length()
。事实上,如果您length()
使用null
引用进行调用,那么 Java 运行时将抛出一个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.
它依赖于这样一个事实,即评估||
是从左到右的,一旦达到第一个true
条件就停止。
回答by Srinivasan.S
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");
}
回答by Vijay Kumar Reddy
You can try like this
你可以这样试试
StringBuilder state = new StringBuilder();
if(StringUtils.isNotBlank(state .toString())){
//this will check for null, " ", ""
}