I have a problem where I have to concatenating two or more characters in two different ways.The method 1 is as follows:
String s = "";
for( int i = 0; i < 10; ++i ){
StringBuffer t = new StringBuffer();
t.append( s );
t.append( i );
t.append( " " );
s = t.toString();
}
The second method is :
StringBuffer b = new StringBuffer();
for( int i = 0; i < 10; ++i ){
b.append( i );
b.append( ' ' );
}
String s = b.toString();
It is mentioned in the book(Programming interview exposed) that the second method is more effectively coded. Why is it so? is it because that the object t of Stringbuffer is not initially repeatedly within the for loop. Need some clarification.