String valueOf versus empty string concatenation

I am working in Java code optimization. I do not understand the difference between a String.valueOf or +"" sign:

 int intVar = 1; String strVar = intVar + ""; String strVar = String.valueOf(intVar); 

What is the difference between lines 2 and 3?

+40
java
Oct. 13 2018-11-11T00:
source share
8 answers
 public void foo(){ int intVar = 5; String strVar = intVar+""; } 

This approach uses StringBuilder to create the resulting 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 calls just the static String method 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()

+54
Oct 13 '11 at 9:59 a.m.
source share

Set yourself a code goal. It:

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

It sounds a lot more like the last to me ... that's why I used String.valueOf . Whenever you can make your code read the same way you would describe what you want to achieve, thatโ€™s good.

Note that this works for all types and will return "null" when passing a null reference instead of throwing a NullPointerException . If you use a class (rather than int , as in this example), and you want it to throw an exception if it was null (for example, because it represents an error), call toString instead of the link.

+18
Oct 13 '11 at 9:57
source share

Using String.valueOf(int) , or better, Integer.toString(int) relatively more efficient for the machine. However, if performance is not critical (in this case, I would not suggest using it). Then ""+ x uses your time much more efficiently. IMHO, this is usually more important. Sometimes massively more important.

In other words, ""+ destroys the object, but Integer.toString() creates a few anyway. Either your time is more important, or you want to avoid creating objects at all costs. You are unlikely to be in the position that the creation of several objects is beautiful, but the creation of another is not.

+7
Oct 13 2018-11-11T00:
source share

I would prefer valueOf() because I find it more readable and explicit.

Any performance issues are micro-optimizations that are not measurable. I would not worry about them until I could measure and see that they made a difference.

+4
Oct 13 2018-11-11T00:
source share

The first line is equivalent

 String strVal = String.valueOf(intVar) + ""; 

so that some additional (and meaningless) work is done. Not sure if the compiler optimizes concatenations with empty string literals. If this is not the case (and looking at @Jigar's answer, this apparently does not), this in turn will become

 String strVal = new StringBuilder().append(String.valueOf(intVar)) .append("").toString(); 

So you really have to use String.valueOf directly.

+3
Oct 13 2018-11-11T00:
source share

Concatenating strings and other variables actually uses String.valueOf() (and StringBuilder ) from below, so the compiler will hopefully discard an empty string and produce the same bytecodes in both cases.

+1
Oct 13 2018-11-11T00:
source share

In terms of optimization, I always prefer String.valueOf() between them. The first is just to hack, trying to trick the conversion of intVar to String , because the + operator.

0
Oct 13 '11 at 9:59 a.m.
source share
 String strVar1 = intVar+""; String strVar2 = String.valueOf(intVar); 

strVar1 is equivalent to strVar2, but using int + emptyString "" is not an elegant way to do this.

using valueOf more efficiently.

-one
Oct 13 '11 at 10:00
source share



All Articles