2

Which approach is better and why ?

If I write

cmissValue = String.valueOf(callDBDatasource.cMiss());

or

cmissValue = "" + callDBDatasource.cMiss();

What should be the approach?

Srinath Ganesh
  • 2,496
  • 2
  • 30
  • 60
  • I would guess long from the title – RNJ Mar 09 '13 at 16:48
  • The second one creates more `String`'s I think. But you could easily prove it yourself. – madth3 Mar 09 '13 at 16:48
  • Well they are both null-safe, as far as i can see, in that cmissValue will not be null. Whether that is meaningful or intended in the context of the problem, I can not say :) – vikingsteve Mar 09 '13 at 16:55

3 Answers3

8
"" + callDBDatasource.cMiss();

Will compile to:

new StringBuilder().append("").append(callDBDatasource.cMiss()).toString();

This will create a new object and is therefore significantly slower. See this question: Is string concatenaion really that slow?

Community
  • 1
  • 1
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
3

This will be useful here (section "Converting numbers to Strings"): http://www.odi.ch/prog/design/newbies.php

Shortly:

    String.valueOf(callDBDatasource.cMiss());
kyticka
  • 604
  • 8
  • 19
  • a simpler summary for beginners please – Srinath Ganesh Mar 09 '13 at 17:17
  • Go to the link and search for "Converting numbers to Strings" it is few lines long. I left it here because it might be usefull for other problems and there was no answer when I started writing it. – kyticka Mar 09 '13 at 18:01
2

For those interested, I've modelled both cases and generated bytecode for them.

Here's a program for the first case:

import java.util.Random;

public class Test1 {
  public static void main(String[] args) {
    long l = new Random().nextLong();
    String s = String.valueOf(l);
    System.out.println(s);
  }
}

...and here's the bytecode:

0:  new             #2; //class java/util/Random
3:  dup
4:  invokespecial   #3; //Method java/util/Random."<init>":()V
7:  invokevirtual   #4; //Method java/util/Random.nextLong:()J
10: lstore_1
11: lload_1
12: invokestatic    #5; //Method java/lang/String.valueOf:(J)Ljava/lang/String;
15: astore_3
16: getstatic       #6; //Field java/lang/System.out:Ljava/io/PrintStream;
19: aload_3
20: invokevirtual   #7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
23: return

Which is what you would expect.

Now, here's a program modelling the second case:

import java.util.Random;

public class Test2 {
  public static void main(String[] args) {
    long l = new Random().nextLong();
    String s = "" + l;
    System.out.println(s);
  }
}

...and here's the bytecode:

0:  new             #2;  //class java/util/Random
3:  dup
4:  invokespecial   #3;  //Method java/util/Random."<init>":()V
7:  invokevirtual   #4;  //Method java/util/Random.nextLong:()J
10: lstore_1
11: new             #5;  //class java/lang/StringBuilder
14: dup
15: invokespecial   #6;  //Method java/lang/StringBuilder."<init>":()V
18: ldc             #7;  //String 
20: invokevirtual   #8;  //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
23: lload_1
24: invokevirtual   #9;  //Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
27: invokevirtual   #10; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
30: astore_3
31: getstatic       #11; //Field java/lang/System.out:Ljava/io/PrintStream;
34: aload_3
35: invokevirtual   #12; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
38: return

As you can see, many more bytecodes are produced than the first case. You can also see (at 11 through 27) that a StringBuilder is used to concatenate the values, as mentioned in the accepted answer.

cdmckay
  • 31,832
  • 25
  • 83
  • 114