I’m doing a computer science project and I need to incorporate StringBuffers. I need help with what .append does and what concatenate means. Somebody told me I can show who is the winner (of my game) by using .append with StringBuffer.
public static void winner ()
{
if (position1 >= 100){
System.out.println("THE WINNER IS " + name1);
}
else if (position2 >= 100){
System.out.println("THE WINNER IS " + name2);
}
}
Instead of having name as strings, can I use StringBuffer to output who won the game?
Thanks to the compiler, you are already using
StringBuilderwhich is the newer, faster version ofStringBuffer.Your code above will compile to the equivalent of:
So, in this case, you would not be accomplishing anything that isn’t already being done for you. Use
StringBuilderwhen you are building aStringin a loop.In your situation, they were probably talking about pre-initializing a single StringBuilder for both cases: