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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:21:24  来源:igfitidea点击:

How to check null on StringBuilder?

javastringbuilder

提问by user1145280

I want to check for null or empty specifically in my code. Does empty and null are same for StringBuilderin Java?

我想在我的代码中专门检查 null 或 empty 。StringBuilderJava中的空和空是否相同?

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, nulland emptyare 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. emptymeans, that there are no characters in the StringBuilder. nullmeans that there is no StringBuilder object at all.

No.empty表示 StringBuilder 中没有字符。null意味着根本没有 StringBuilder 对象。

A variable is only nullif 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 StringBuilderand 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 ifcondition will throw NullPointerException, if the stateis null. So ifshould be nested with if else

如果为空,您的第二个if条件将抛出。所以应该嵌套NullPointerExceptionstateifif else

回答by Bathsheba

In Java, nullis a reference literal. If a variable is nullthen is not referring to anything.

在 Java 中,null引用文字。如果一个变量是null那么不是指任何东西。

So, if you have StringBuilder s = null, that means that sis of type StringBuilderbut it is not referring to a StringBuilderinstance.

所以,如果你有StringBuilder s = null,那意味着它s是类型的,StringBuilder但它不是指一个StringBuilder实例。

If you have a non-nullreference then you are free to call methodson the referred object. In the StringBuilderclass, one such method is length(). In fact if you were to call length()using a nullreference 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 truecondition.

它依赖于这样一个事实,即评估||是从左到右的,一旦达到第一个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, " ", ""
}