I am not able to understand the following behavior of StringBuilder when NULL objects are appended to an instance:
public class StringBufferTest {
/**
* @param args
*/
public static void main(String[] args) {
String nullOb = null;
StringBuilder lsb = new StringBuilder();
lsb.append("Hello World");
System.out.println("Length is: " + lsb.length());// Prints 11. Correct
lsb.setLength(0);
System.out.println("Before assigning null" + lsb.length());
lsb.append(nullOb);
System.out.println("Length now is:" + lsb.length()); // Prints 4. ???
}
}
The last print statement does not print 0. Can anyone please help me understand the behavior?
From the StringBuffer API –
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuffer.html#append(java.lang.String)
This should explain the length as 4.