user1145280 user1145280 - 1 month ago 12
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

Answer

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