Search code examples
javastringstringbuilderstringbuffer

Why does StringBuffer allows StringBuffer as argument to its constructor even when StringBuffer doesn't have a StringBuffer constructor?


Why is the below line not giving a compile time error?

StringBuffer sb = new StringBuffer(new StringBuffer());

I did enough online search but couldn't find the reason. Not just this it is also allowing StringBuider argument StringBuffer sb2 = new StringBuffer(new StringBuilder());

Someone please help me.


Solution

  • Because StringBuffer and StringBuilder implements the interface CharSequence and the StringBuffer has a constructor for that interface.