java - StringBuilder() vs StringBuilder(null) vs StringBuilder("") -


public class testmystringbuilderii {

public static void main(string[] args) {     stringbuilder sb = new stringbuilder("hello");     stringbuilder sb1 = new stringbuilder("world");     stringbuilder sb2 = new stringbuilder();     //stringbuilder sb3 = new stringbuilder(null);     stringbuilder sb4 = new stringbuilder("");      system.out.println(sb);     system.out.println(sb.length());     system.out.println(sb.append(sb1));     system.out.println(sb.append(sb2));     //system.out.println(sb.append(sb3)); } 

}

the sb3 = new stringbuilder(null) results in nullpointerexception. now, difference between stringbuilder(), stringbuilder(""), , stringbuilder(null)?

a) system.out.println(sb2); printing empty space when actual characters ("null") expected appended (according abstaractstringclass definition) - why?

b) system.out.println(sb2.length()); printing 0, because sb not "null" (meaning thing/type length of zero?)

c) system.out.println(sb4); printing empty space , system.out.println(sb4.length()); printing 0 (assuming sb4 of string because of double quotes used argument, 0 length) - correct?

not sure if question formulated correctly, appreciate if can understand thinking/confusion here , clarify these bit.

new stringbuilder("abc") convenience overload new stringbuilder().append("abc") except javadoc says:

the initial capacity of string builder 16 plus length of string argument.

this intended create larger initial backing array, improved performance, though minuscule.

since cannot take length of null value, nullpointerexception.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -