1

I've read about StringBuilder class in Java and I'm wondering, what is more efficient way to do some task:
Using "+" concatenation:

 String result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
       "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:y=\"http://api.yandex.ru/yaru/\">"
       + "<title>" + (et_title.getText().toString()) + "</title>" +
       "<y:access>"+ privacymode +"</y:access>" +
       "<category scheme=\"urn:ya.ru:posttypes\" term=\"link\"/>"
       + "<y:meta>" + "<y:url>" + (et_link.getText().toString()) + "</y:url>" +
       "</y:meta>" +
       "<content>" + signature_select() + "</content>"
       + "</entry>";

or

 StringBuilder sb = new StringBuilder();                     
     sb.append( "<?xml version=\"1.0\" encoding=\"utf-8\"?>")
       .append("<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:y=\"http://api.yandex.ru/yaru/\">")
       .append("<title>")
       .append(et_title.getText().toString())
       .append("</title>")
       .append("<y:access>"+ privacymode +"</y:access>")
       .append( "<category scheme=\"urn:ya.ru:posttypes\" term=\"link\"/>")
       .append("<y:meta>" + "<y:url>" + (et_link.getText().toString()) + "</y:url>" + "</y:meta>")
       .append( "<content>" + signature_select() + "</content>")
       .append("</entry>");
String result = sb.toString();
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • From experience,use the former. Let the compiler do the optimizations for you. – Luiggi Mendoza May 10 '14 at 16:00
  • This looks to be worrying about premature optimization. If this is a one-off bit of code and not being called within a tight loop, why worry about optimizing? Having said that, most often the xml text would be found in a text file and not hard-coded in the program. – Hovercraft Full Of Eels May 10 '14 at 16:11

2 Answers2

0

This looks to be worrying about premature optimization. If this is a one-off bit of code and not being called within a tight loop, why worry about optimizing?

Having said that, most often the xml text would be found in a text file and not hard-coded in the program, and then you'll use a loop and a StringBuilder, i.e.,

  StringBuilder xmlSb = new StringBuilder();
  Scanner xmlScanner = new Scanner(Foo.class.getResourceAsStream(RESOURCE));
  while (xmlScanner.hasNextLine()) {
     xmlSb.append(xmlScanner.nextLine());
  }
  if (xmlScanner != null) {
     xmlScanner.close();
  }

  // parse and use your xml text here
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Edit: Looks like the Java compiler will optimize the String concatenation (+) version, so both versions will have the same performance, with the advantage to the first one being more readable. Leaving my previous answer for historic purposes.

Thanks for the comments!


The StringBuilder version is better.

I'd also break the other concatenations you have in the StringBuilder version, like for example:

.append("<y:access>"+ privacymode +"</y:access>")

as:

.append("<y:access>")
.append(privacymode)
.append("</y:access>")

The biggest advantage of using StringBuilder is that you avoid allocating new Strings for each concatenation, whereas StringBuilder will only allocate and resize its internal char array when necessary. It would be even better if you knew the size of the final string. Then you could initialize the StringBuilder with its final size and avoid any extra memory allocation.

  • Sorry, it wasn't me who voted down. Why breaking inner concatenations? –  May 10 '14 at 16:26
  • @Kondra007 Because the inner concatenations would create two new Strings (in my example) before passing the argument into the append function. – Tércio de Almeida May 10 '14 at 16:28
  • I would like to understand why someone voted me down... – Tércio de Almeida May 10 '14 at 16:29
  • Down-vote nullified by an up-vote. – Hovercraft Full Of Eels May 10 '14 at 16:56
  • `The biggest advantage of using StringBuilder is that you avoid allocating new Strings for each concatenation,` I didn't down vote but that statement is not correct (and maybe deserves a downvote?). Read the link pointing to the duplicate question for more information. – camickr May 10 '14 at 18:12
  • I downvoted you because the bytecode generated, and therefore the performance, of these versions will be exactly equivalent...except that string constants will be joined better in the `+` version, e.g. `""` and `""` will be merged into a single constant string at compile time. – Louis Wasserman May 10 '14 at 19:10
  • @LouisWasserman Thank you very much Louis and camickr for explaining why my answer was wrong. I've edited it to consider the java compiler optimization. – Tércio de Almeida May 10 '14 at 23:17