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

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

Making Empty C++ Project: General exception (Exception from HRESULT:0x80131500) Visual Studio Community 2015 -

How to fix java warning for "The value of the local variable is not used " -