20

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?

Braiam
  • 1
  • 11
  • 47
  • 78
user1145280
  • 371
  • 1
  • 2
  • 10
  • It's not the same thing everywhere, not only `StringBuilder`. – Maroun Feb 04 '14 at 13:17
  • If you run static analysis for your code it will complain that the code in the if(state == null) block will never be executed as "state" can never be null on that line (since it has been just initialized to a non-null value). That should give a pointer to null and "" being quite different things. – Torben Feb 04 '14 at 13:18
  • thx..i have missed one more line in my code..i will edit it and put some more details. – user1145280 Feb 04 '14 at 13:21
  • (since it has been just initialized to a non-null value).? where exactly it becoming non-null? – user1145280 Feb 04 '14 at 13:39
  • StringBuilder state = new StringBuilder(); this makes state non-null – jamp Feb 04 '14 at 13:47

6 Answers6

33

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");
}
nikodaemus
  • 1,918
  • 3
  • 21
  • 32
Kick
  • 4,823
  • 3
  • 22
  • 29
  • 3
    does StringBuilder state = new StringBuilder() and StringBuilder state = new StringBuilder("") are same?? – user1145280 Feb 04 '14 at 13:34
  • 1
    This answer is wrong. The second example in it does not work because emptyBuilder i not null which makes the System.out.printLn statement dead code. – Marquez Apr 19 '17 at 17:35
  • @Marquez The null check was completely unnecessary because the `StringBuilder` is defined in the previous statement. I submitted an edit to remove it, which should fix the check. – Abandoned Cart Apr 16 '18 at 07:26
  • 1
    `emptyBuilder.toString().equals(""))` should be `emptyBuilder.toString().isEmpty())` – Fiddle Freak May 31 '18 at 21:46
12

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
5

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

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
1

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.

markusthoemmes
  • 3,080
  • 14
  • 23
0

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");
}
Srinivasan.S
  • 3,065
  • 1
  • 24
  • 15
0

You can try like this

StringBuilder state = new StringBuilder();
if(StringUtils.isNotBlank(state .toString())){   
//this will check for null, " ", ""
}
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69