NEW STRINGBUFFER (int N) trap

zhaozj2021-02-16  53

StringBuffer SB = New StringBuffer (2004);

Sb.append ("-");

Sb.append (6);

Sb.append ("-");

Sb.append (14);

System.out.println (SB);

What do you guess?

"2004-6-14"

Wrong, output "-6-14"

We see StringBuffer overloaded Append (),

See the effect of append (int),

See New StringBuffer (String S)

Equivalent

{

?? StringBuffer SB = new stringbuffer ();

?? sb.append (s);

}

Just think New StringBuffer (INT N);

Equivalent to:

{

?? StringBuffer SB = new stringbuffer ();

?? sb.append (n);

}

In fact, it is not.

New StringBuffer (int N); represents a StringBuffer, and initializes its length to N,

It is still empty.

Take a look at the documentation:

??? / ** ????? * constructs a string buffer with no character, init,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ?? length ?? The initial capacity. ???? * @Exception? NegativeAraysizeException? if the length ???? * ?????????????? argument is less 0.??? ? * /

转载请注明原文地址:https://www.9cbs.com/read-21431.html

New Post(0)