How StringBuffer is used with .append

I am involved in computer science projects and I need to enable StringBuffer s. I need help with what .append does and what concatenate means. Someone told me that I can show who is the winner (of my game) 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 the name as a string, can I use a StringBuffer to output who won the game?

+4
source share
7 answers

Thanks to the compiler, you are already using StringBuilder , which is a newer, faster version of StringBuffer .

Your code above will be compiled with the equivalent:

 public static void winner () { if (position1 >= 100){ System.out.println(new StringBuilder("THE WINNER IS ").append(name1).toString()); } else if (position2 >= 100){ System.out.println(new StringBuilder("THE WINNER IS ").append(name2).toString()); } } 

So, in this case, you will not achieve what has not yet been done for you. Use StringBuilder when you build String in a loop.

In your situation, they probably talked about pre-initializing one StringBuilder for both cases:

 public static void winner() { StringBuilder out = new StringBuilder("THE WINNER IS "); if (position1 >= 100) { out.append(name1); } else if (position2 >= 100 { out.append(name2); } else { return; // Preserve previous behavior just in case, remove this if it not needed } System.out.println(out); } 
+6
source

You probably want something like:

  public static void winner () { StringBuffer sb = new StringBuffer("THE WINNER IS "); if (position1 >= 100) System.out.println(sb.append(name1).toString()); else if (position2 >= 100) System.out.println(sb.append(name2).toString()); } 

BUT, as a rule, StringBuilder is preferable to StringBuffer, since access to StringBuffer is synchronized.

+2
source

You can, since System.out.println automatically converts its argument to String , which will result in the contents of the StringBuffer . You will mainly use it when you need to iterate a dataset.

Using the append method append you can add data to the buffer:

 String[] values = { 'string value', 'testing' }; StringBuffer sb = new StringBuffer(); for(String value: values) { sb.append(value); } 

will result in β†’ string valuetesting

Note: Instead, it is better to consider StringBuilder .

+1
source

Small incremental lines and concats usually do not see much benefit from using StringBuilder , but just keep in mind that adding to a string has 3 operations, 1 new distribution consisting of the size of both added lines and 2 copies of the operation to copy the content (which means now you have 3 lines in the memory), while StringBuilder you simply add and change the size to a fixed-width buffer, which in most cases requires only 1 copy (copy of the amortized cost, sometimes you need to change the size of which is n inimaet 2). If you add a lot to 1 line, I would recommend using StringBuilder , but for concatentations like yours, you shouldn't see any performance loss (at least measurable).

+1
source

The idea of ​​using StringBuffer or StringBuilder is to avoid re-concatenating strings as shown below:

 String result = ""; for (int i = 0; i < 200; i++) { result = result + i + ","; } 

This is inefficient, in each loop one String object is created (created by java)

Using StringBuilder / StringBuffer:

 StringBuilder buf = new StringBuilder(200); // init with a estimation of length has advantage for (int i = 0; i < 200; i++) { buf.append(i).append(","); } String result = buf.toString(); 

This avoids the unnecessary creation of lines in each cycle, it simply fills the buffer with characters and, if necessary, changes the size of the buffer.

However, in your case it is not worth the effort.

+1
source

Yes, you can. As everyone noted, StringBuffer.toString () will output the contents as a string. The question you need to ask yourself is whether you really care about what you use, or if you specifically want to use StringBuffer to find out something.

For your operation, one is as good as the other. If you really want to do what your friend tells you, I think the people here clearly answered.

Or you can tell your friend: β€œIt doesn’t matter. In my case, it’s as good as in the other” :)

0
source

To get a synchronized version of a string, it is recommended to use StringBuffer, where high thread safety is required. For unsynchronized use, the StringBuilder class, where thread safety is not required, and our application will give better performance.

0
source

All Articles