-2

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.

Vagabond
  • 251
  • 1
  • 2
  • 11
  • Why does this question received a negative point? I have a small doubt about which I just need some more clarification. – Vagabond May 01 '19 at 01:31
  • Most of the time folks won't explain why they vote a question down. But are you certain you copied those examples exactly? – WJS May 01 '19 at 02:41
  • These examples from a book which I have mentioned. I was reading that book for my interview preparation. It is not a homework assignment or question. – Vagabond May 01 '19 at 02:49
  • The second one is more efficient by far. It creates a single instance of StringBuffer where as the first creates one for every iteration. – WJS May 01 '19 at 03:00
  • Must be a really *old* book, using `StringBuffer` --- See [Difference between StringBuilder and StringBuffer](https://stackoverflow.com/q/355089/5221149) – Andreas May 01 '19 at 03:23
  • I think that the community of stack-overflow should be more open to new developers..Everyone should be allowed to ask whatever they want. I believe that should be the sole purpose of stack-overflow. It should not be a platform for judging anyone's question. – Vagabond May 01 '19 at 03:53

3 Answers3

1

Method 1 defines a new StringBuffer and calls toString() every iteration, which uses more operations and memory compared to method 2 where a StringBuffer is created just once, and toString() is called just once

Jacob Hicks
  • 63
  • 1
  • 7
1

Here you go. My comments identify the added overhead of the first method.


String s = "";
for( int i = 0; i < 10; ++i ){

// create this for each iteration
StringBuffer t = new StringBuffer();

// extra method call to add string in progress
t.append( s ); 

t.append( i );

// more overhead appending string than a char
t.append( " " );

// convert StringBuffer to a string to append back in later
s = t.toString();
}

And for both methods, StringBuilder should be preferred over StringBuffer. The latter is preferred when working with threads because it is synchronized. So if you're not working with threads, use StringBuilder.

WJS
  • 22,083
  • 3
  • 14
  • 32
0

Your method 1 is consuming lots of unnecessary memory and it is not required as compare to method 2. Even you can use StringBuilder instead of StringBuffer , you can read the javadoc as why StringBuilder is faster n better than StringBuffer and where it is required to use what.