views:

1180

answers:

5

Is there a perceptable difference between using String.Format and string concatenation in Java?

I tend to use String.format but occasionally will slip and use a concat, I was wondering if one was better than the other.

The way I see it String.Format gives you more power in "formatting" the string and concatenation means you don't have to worry about accidentally putting in an extra %s or missing one out.

String.format is also shorter.

Which one is more readable depends on how your head works.

+11  A: 

I'd suggest that it is better practice to use String.format. The main reason is that String.format can be more easily localised with text loaded from resource files whereas concatenation can't be localised without producing a new executable with different code for each language.

If you plan on your app being localisable you should also get into the habit of specifying argument positions for your format tokens as well:

"Hello %1$s the time is %2$t"

This can then be localised and have the name and time tokens swapped without requiring a recompile of the executable to account for the different ordering. With argument positions you can also re-use the same argument without passing it into the function twice:

String.format("Hello %1$s, your name is %1$s and the time is %2$t", name, time)
workmad3
+1 did not know about argument ordering.
Omar Kooheji
+2  A: 

Which one is more readable depends on how your head works.

You got your answer right there.

It's a matter of personal taste.

String concatenation is marginally faster, I suppose, but that should be negligible.

Thilo
I agree. Thinking about performance differences here is mainly just premature optimisation - in the unlikely event that profiling shows there's a problem here, then worry about it.
Jonik
It's only really a matter of personal taste if the project is small and never intended to be internationalised in any meaningful sense. Otherwise String.format wins out over concatenation in every way.
workmad3
I disagree. No matter how large the project is, you're hardly going to localise every string that's ever constructed within it. In other words, it depends on the situation (what are the strings used for).
Jonik
+3  A: 

String.format() is more than just concatenating strings. For example, you can display numbers is a specific locale using String.format().

However, if you don't care about localisation, functionally, there is no difference. Maybe the one is faster than the other, but in most cases it will be neglectable..

Fortega
+1  A: 

I haven't done any specific benchmarks, but I would think that concatenation may be faster. String.format() creates a new Formatter which, in turn, creates a new StringBuilder (with a size of only 16 chars). That's a fair amount of overhead especially if you are formatting a longer string and StringBuilder keeps having to resize.

However, concatenation is less useful and harder to read. As always, it's worth doing a benchmark on your code to see which is better. The differences may be negligible in server app after your resource bundles, locales, etc are loaded in memory and the code is JITted.

Maybe as a best practice, it would be a good idea to create your own Formatter with a properly sized StringBuilder (Appendable) and Locale and use that if you have a lot of formatting to do.

AngerClown
+2  A: 

About performance:

 public static void main(String[] args) throws Exception {      
  long start = System.currentTimeMillis();
  for( int i=0;i<1000000; i++){
      String s = "Hi " + i + "; Hi to you " + i*2;
  }
  long end = System.currentTimeMillis();
  System.out.println("Concatenation = " + ((end - start)) + " millisecond") ;

  start = System.currentTimeMillis();
     for( int i=0;i<1000000; i++){
         String s = String.format( "Hi %s; Hi to you %s",i, + i*2);
     }
     end = System.currentTimeMillis();
     System.out.println("Format = " + ((end - start)) + " millisecond");
   }

give tu me:

  • Concatenation = 265 millisecond
  • Format = 4141 millisecond

so Concatenation is mutch more faster than Formatting

Icaro
It is certainly faster. But who's going to call it one million times per second?
BalusC
It depends to your requirements. I'have just write a very long time running String manipolation batch application ant it call string concatenation so many times! :)
Icaro