2

What is the difference between these code blocks - is there any difference in performance or memory or other?

the first code is:

String name = "Jack";
String familyName = "Hunter";

String result = String.format("Your name is %s %s",name, familyName);

and second code is:

String name = "Jack";
String familyName = "Hunter";

String result = "Your name is " + name + " " + familyName;

thank you for attentions ;-)

Elyas Hadizadeh
  • 3,289
  • 8
  • 40
  • 54
  • There are some results here: [Should I use Java's String.format() if performance is important?](http://stackoverflow.com/questions/513600/should-i-use-javas-string-format-if-performance-is-important) – Obicere Dec 07 '14 at 07:40

2 Answers2

2

There's no difference in terms of the output it produces. The result variable will have the same contents in each case.

But String.format() is there so that you can do more powerful formatting, including specifying number of decimal places for floating point numbers, and the like.

I would expect the String.format() version to be relatively slow, simply because it's more powerful, and has to check the contents of the format string before performing essentially the same operation as you're doing in your second version. In other words, it has to spend some time working out what you want to do, whereas in the second version, the compiler can work it out for itself.

But, really, choosing one of your snippets over the other here for performance reasons looks like the kind of micro-optimisation that will almost never pay off. Unless you have a clear need for performance (i.e., this is running in a tight loop for something performance-critical), you should go for the one that's more readable.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • To be fair, the performance of the second code should be a lot faster. And the second code will be changed to utilize a `StringBuilder`, so the performance is identical. – Obicere Dec 07 '14 at 07:45
  • @Obicere do you mean that the compiler will internally use a `StringBuilder` for a chain of `String`s concatenated together in one go? That does seem like an obvious optimisation, though I wasn't aware that the compiler did it. – chiastic-security Dec 07 '14 at 07:46
  • yep! Just one of the great things about the Java compiler. – Obicere Dec 07 '14 at 07:48
  • @Obicere Ah, nice. OK, I've removed that suggestion! – chiastic-security Dec 07 '14 at 07:55
  • @Obicere Excuse me, i want to know did i get it true or not? You mean when we use this code : result = name + " " + familyName; java compiler automatically invoke(call) StringBuilder's method for contacting these strings together? – Elyas Hadizadeh Dec 07 '14 at 10:59
  • 1
    @ElyasHadizadeh rather, the compiler will replace the code with the `StringBuilder` equivalent code. – Obicere Dec 08 '14 at 20:01
  • @Obicere: are you sure? so i think for enhancing the performance instead of using + for contacting the string, it's better to use `StringBuilder`. am i right? – Elyas Hadizadeh Dec 09 '14 at 06:53
  • @ElyasHadizadeh yep. Consider this: `"str" + "ing" + "test"`. Without the string builder, it must first create a new string, and copy all the information over, resulting in `"string" + "test"`. Then, copy all the new information over as well as the previous longer string, to form `"stringtest"`. `StringBuilder` avoids having to copy all the previous information every concatenation. – Obicere Dec 10 '14 at 00:29
  • @ElyasHadizadeh good :) please do click the tick to mark as accepted answer if you found it useful (gives you some rep points too). – chiastic-security Dec 10 '14 at 07:54
  • @chiastic-security: yeah why not,actually i forgot to do it ;-) – Elyas Hadizadeh Dec 10 '14 at 11:27
0

But using "$" better than using string.Format i think. Because you can do the things, that string.Format can do.

Benefits of the "$":

  1. Less code
  2. Better performance

at c#

azz
  • 1