How to check null on StringBuilder?

前端 未结 6 2204
自闭症患者
自闭症患者 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: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");
    }
    

提交回复
热议问题