Well, as a side note, it depends on what x is. If x is primitive in Java, you should call .toString()using one of your shells, for example
Integer.toString(x)
I would say that using toString () is usually better, because x + "", at least in Java, says you want to add two lines together.
Like in this example:
public static void main(String[] args)
{
int x = 3;
String s = x + "";
}
It ends in bytecode like:
public static void main(java.lang.String[]);
Code:
0: iconst_3
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/lang/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/lang/String;
21: astore_2
22: return
Therefore, he must create a StringBuilder to add "" and the value of String x together. Although there is not much loss in efficiency, it is not easy to use the toString function.
Compare using toString:
public static void main(String[] args)
{
int x = 3;
String s = Integer.toString(x);
}
What ends up like:
public static void main(java.lang.String[]);
Code:
0: iconst_3
1: istore_1
2: iload_1
3: invokestatic #2; //Method java/lang/Integer.toString:(I)Ljava/lang/String;
6: astore_2
7: return
, .toString , - , String x, x + " - String x, " ".
:
-, # , . , # .ToString() , , , .