user1145280 user1145280 - 2 months ago 26
Java Question

how to check null on StringBuilder?

I am new to java, 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?

thanks for help


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");