4

I want to append an integer to a string. Is there a difference between doing:

String str = "Number: " + myInt;

and

String str = "Number: " + Integer.toString(myInt);

In other words, should I bother using the Integer.toString() method, when not necessary?

Edit: I am not wondering "How to convert an integer to string", but "if I am required to use a certain method, to convert".

Victor2748
  • 4,149
  • 13
  • 52
  • 89

2 Answers2

5

There's no difference. The compiler (Oracle JVM 1.8) transform both snippets to

(new StringBuilder()).append("Number: ").append(myInt).toString();

Personally, I wouldn't use Integer.toString() as it adds noise to the code, and that doesn't provide clarity nor readability.

Edit

I made a mistake on the original answer and described that there would be a minor difference (see the answer history if you want!)

Augusto
  • 28,839
  • 5
  • 58
  • 88
  • Another question would be how the `append(int)` method converts the `int` to a `String`, and hence whether or not a direct call to `Integer.toString(int)` would be better for performance. – SamTebbs33 Aug 08 '15 at 23:00
  • I trust that the guys who build the JVM know about performance, and I reckon that they did this on purpose as there might be a performance improvement from using `StringBuilder.append(int)`... but I cannot comment on this, as I just don't know. – Augusto Aug 08 '15 at 23:05
  • their is no reason for speculation because of of ignorance here, all the JDK source is available –  Aug 09 '15 at 00:43
  • @JarrodRoberson I think you're missing the compiler optimisation, which is not 'there' if in the source code. My original (and wrong) answer was based purely on the source code. – Augusto Aug 10 '15 at 18:27
2

Clarify on Augusto's answer: You can run a simple benchmark test.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Race Started"); 
    int []noArr = new int[100];
    for(int i=0;i<100;i++)
        noArr[i] = i+1;

    String str = "";
    long startedTime = System.nanoTime();
    for(int j=0;j<100;j++)
        str = str + noArr[j];

    long runningTime = System.nanoTime() - startedTime;
    System.out.println("Concatenation String + int took : "+runningTime);

    startedTime = System.nanoTime();
    String str2 = "";
    for(int h=0;h<100;h++)
        str2 = str2 + Integer.toString(noArr[h]);
    runningTime = System.nanoTime() - startedTime;
    System.out.println("Concatenation String with Wrapper obj took : "+runningTime);

    startedTime = System.nanoTime();
    StringBuilder sb = new StringBuilder("");
    for(int k=0;k<100;k++)
        sb.append(k);

    runningTime = System.nanoTime() - startedTime;
    System.out.println("StringBuilder took : "+runningTime);

}

Summary {Time in nanoSeconds}

Race Started (These are rough values; but you can see the huge differences)

  • Concatenation String + int took : 250555
  • Concatenation String with Wrapper obj took : 485413
  • StringBuilder took : 98411
Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62