How to check null on StringBuilder?

前端 未结 6 2154
自闭症患者
自闭症患者 2021-02-18 18:39

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         


        
相关标签:
6条回答
  • 2021-02-18 19:15

    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.

    0 讨论(0)
  • 2021-02-18 19:15

    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

    0 讨论(0)
  • 2021-02-18 19:26

    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");
    }
    
    0 讨论(0)
  • 2021-02-18 19:29

    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.

    0 讨论(0)
  • 2021-02-18 19:37

    You can try like this

    StringBuilder state = new StringBuilder();
    if(StringUtils.isNotBlank(state .toString())){   
    //this will check for null, " ", ""
    }
    
    0 讨论(0)
  • 2021-02-18 19:42

    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");
    }
    
    0 讨论(0)
提交回复
热议问题