Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better practice to use String.format over string Concatenation in Java?

Is there a perceptible difference between using String.format and String concatenation in Java?

I tend to use String.format but occasionally will slip and use a concatenation. 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.

like image 605
Omar Kooheji Avatar asked May 29 '09 10:05

Omar Kooheji


People also ask

Which is the efficient way of concatenating the string in Java?

StringBuilder is a widely used and recommended way to concatenate two strings in Java. It is mutable, unlike string, meaning that we can change the value of the object.

Is it good to use string format?

tl;dr. Avoid using String. format() when possible. It is slow and difficult to read when you have more than two variables.

Is string interpolation more efficient than concatenation?

When concatenating strings in a single expression, the compiler seems to do the same optimization as with string interpolation. This means there's no advantage in using StringBuilder. Go with whatever is more readable .


2 Answers

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) 
like image 125
workmad3 Avatar answered Oct 07 '22 02:10

workmad3


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"); } 

The timing results are as follows:

  • Concatenation = 265 millisecond
  • Format = 4141 millisecond

Therefore, concatenation is much faster than String.format.

like image 24
Icaro Avatar answered Oct 07 '22 01:10

Icaro