-1

What is more efficient to use for appending data to string

String a= string.format("xx %s xx",yyy);

or

String b =xx+yyy+xxx; 

?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
15412s
  • 3,298
  • 5
  • 28
  • 38
  • 7
    `format` is definitely less efficient. `format` needs to scan through the format string, find where there are placeholders and replace them. – Sotirios Delimanolis Dec 29 '14 at 17:57
  • Linked issue: http://stackoverflow.com/questions/513600/should-i-use-javas-string-format-if-performance-is-important – Beri Dec 29 '14 at 17:58
  • 6
    `var prematureOptimization = Math.sqrt(Evil.all())` – Mike G Dec 29 '14 at 17:59
  • Unless performance is a critical aspect, the difference is negligeable and the important matter is readability. And for that, I prefer by far `String.format` in most of the cases – Dici Dec 29 '14 at 18:00
  • @mikeTheLiar: Totally off topic comment... You don't know the situation of the OP. (Well, as your name indicates, it was probably a lie) – Martijn Courteaux Dec 29 '14 at 18:00
  • The following thread will answer both for `Efficiency` and `Best-practice` questions concerning those issues. http://stackoverflow.com/questions/925423/is-it-better-practice-to-use-string-format-over-string-concatenation-in-java – Orel Eraki Dec 29 '14 at 18:00
  • @mar I can read between the lines, but I see your point. – Mike G Dec 29 '14 at 18:02

2 Answers2

2

String a= string.format("xx %s xx",yyy); It will replace yyy to %s for that it require pare whole string and then create new string object. so less efficient then concatenation xx+yyy+xxx;.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
  • Do you have some evidence that the compiler does not translate the first snippet into the second ? – Dici Dec 29 '14 at 18:06
  • @Dici Nothing in the JLS requires it (or even suggests) to do so. – Sotirios Delimanolis Dec 29 '14 at 18:07
  • @SotiriosDelimanolis now that I think about it, `format` is not a simple replacement, it is also a type/format check (`%d, %f`) and some exceptions may be thrown, so there definitely are additional computations. My bad ! – Dici Dec 29 '14 at 18:15
  • Java's `String.format` works like so: 1. **It parses the format string,** exploding into a list of format chunks 2. It **iterates** the format chunks, rendering into a `StringBuilder`, which is basically an array that resizes itself as necessary, by copying into a new array. this is necessary because we don't yet know how large to allocate the final `String.` 3. `StringBuilder.toString()` copies his internal buffer into a new String So, It is not efficient then simple concatenation – atish shimpi Dec 29 '14 at 18:22
2

Appending using operators is generally more efficient. Format has to take the string and find "%"'s and so, and replace them with corresponding values. Appending is simpler, and shorter to type!

Imagine you are the compiler.

Go through the string to find the %s symbol. Replace it with the value there. Then concatenate.

versus

Concatenate.

Community
  • 1
  • 1
Orange Mushroom
  • 431
  • 4
  • 16
  • 1
    Doesn't "efficient" refers to the runtime efficiency ? I'm wondering if the compiled code is really different in those cases. – Dici Dec 29 '14 at 18:06
  • I mean that it is maybe like comparing `String s = "a" + "b"` and `StringBuilder sb = new StringBuilder("a"); sb.append("b")`. One could think that the second is more efficient, but the compiler actually generates the same bytecode for both (if I'm correct) – Dici Dec 29 '14 at 18:08
  • 2
    I see, when I referred to efficiency, I included factors such as runtime efficiency, how much do you have to type, and readability, which are common ways to refer to efficiency. This answer has some more info on it. http://stackoverflow.com/questions/513600/should-i-use-javas-string-format-if-performance-is-important – Orange Mushroom Dec 29 '14 at 18:13