String valueOf vs concatenation with empty string

后端 未结 10 1555
说谎
说谎 2020-11-27 05:05

I am working in Java code optimization. I\'m unclear about the difference between String.valueOf or the +\"\" sign:

int intVar = 1;         


        
相关标签:
10条回答
  • 2020-11-27 05:39

    Concatenating Strings and other variables actually uses String.valueOf() (and StringBuilder) underneath, so the compiler will hopefully discard the empty String and produce the same bytecodes in both cases.

    0 讨论(0)
  • 2020-11-27 05:44
    String strVar1 = intVar+"";
    String strVar2 = String.valueOf(intVar);
    

    strVar1 is equvalent to strVar2, but using int+emptyString "" is not elegant way to do it.

    using valueOf is more effective.

    0 讨论(0)
  • 2020-11-27 05:49

    Using String.valueOf(int), or better, Integer.toString(int) is relatively more efficient for the machine. However, unless performance is critical (in which case I wouldn't suggest you use either) Then ""+ x is much more efficient use of your time. IMHO, this is usually more important. Sometimes massively more important.

    In other words, ""+ wastes an object, but Integer.toString() creates several anyway. Either your time is more important or you want to avoid creating objects at all costs. You are highly unlikely to be in the position that creating several objects is fine, but creating one more is not.

    0 讨论(0)
  • 2020-11-27 05:50

    From the point of optimization , I will always prefer the String.valueOf() between the two. The first one is just a hack , trying to trick the conversion of the intVar into a String because the + operator.

    0 讨论(0)
  • 2020-11-27 05:52
    public void foo(){
    int intVar = 5;
    String strVar = intVar+"";    
    }
    

    This approach uses StringBuilder to create resultant String

    public void foo();
      Code:
       0:   iconst_5
       1:   istore_1
       2:   new     #2; //class java/lang/StringBuilder
       5:   dup
       6:   invokespecial   #3; //Method java/lang/StringBuilder."<init>":()V
       9:   iload_1
       10:  invokevirtual   #4; //Method java/lang/StringBuilder.append:(I)Ljava/lan
    g/StringBuilder;
       13:  ldc     #5; //String
       15:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/
    String;)Ljava/lang/StringBuilder;
       18:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/la
    ng/String;
       21:  astore_2
       22:  return
    

    public void bar(){
    int intVar = 5;
    String strVar = String.valueOf(intVar);
    }
    

    This approach invokes simply a static method of String to get the String version of int

    public void bar();
      Code:
       0:   iconst_5
       1:   istore_1
       2:   iload_1
       3:   invokestatic    #8; //Method java/lang/String.valueOf:(I)Ljava/lang/Stri
    ng;
       6:   astore_2
       7:   return
    

    which in turn calls Integer.toString()

    0 讨论(0)
  • 2020-11-27 05:52

    Ask yourself the purpose of the code. Is it to:

    • Concatenate an empty string with a value
    • Convert a value to a string

    It sounds much more like the latter to me... which is why I'd use String.valueOf. Whenever you can make your code read in the same way as you'd describe what you want to achieve, that's a good thing.

    Note that this works for all types, and will return "null" when passed a null reference rather than throwing a NullPointerException. If you're using a class (not an int as in this example) and you want it to throw an exception if it's null (e.g. because that represents a bug), call toString on the reference instead.

    0 讨论(0)
提交回复
热议问题