" x.toString()和x + QUOT之间差值; [英] Difference between x.toString() and x+""
问题描述
早在大学我PROFS之一。教我们只是做 X +,
从基本类型为字符串的快速转换。结果
我不记得是在哪我班让他对一些Java和C ++课程(我还没有使用,也有一段时间了),我却用它在C#/。NET现在这就是我主要发展在最近。</ p>
是否有优势,使用的ToString()
在 +
基本类型这种以int,小数,浮...?
什么情况下会的ToString()
更好
请注意:我被的ToString()
,以及,那只是教授推荐的 +
,因为它是短,我刚才已经做了,从那以后没有质疑它。
那么,作为一个侧面说明,这取决于什么的X.如果x是Java原始,你有的ToString()
使用其包装的一个,叫像
Integer.toString(X)
我会说使用的toString()一般都比较好,因为X + ,至少在Java中,是说要两个字符串追加在一起
就像这个例子:
公共静态无效的主要(字串[] args)
{
INT X = 3;
的String = X +;
}
这结束了,在字节码,如:
公共静态无效的主要(java.lang.String中[]);
码:
0:iconst_3
1:istore_1
2:新的#2; //级Java /郎/ StringBuilder的
5:DUP
6:invokespecial#3; //方法的Java /郎/ StringBuilder的<&初始化GT;:()V
9:iload_1
10:invokevirtual#4; //方法的Java /郎/ StringBuilder.append:(I)Ljava /朗/ StringBuilder的;
13:最不发达国家#5; //字符串
15:invokevirtual#6; //方法的Java /郎/ StringBuilder.append:(Ljava /朗/字符串;)Ljava /朗/ StringBuilder的;
18:invokevirtual#7; //方法的Java /郎/ StringBuilder.toString :()Ljava /朗/字符串;
21:astore_2
22:返回
因此,必须创建一个StringBuilder追加,一起x的字符串值。虽然失去了效率并不算多,但不是太多,只是使用toString功能
使用的toString比较:
公共静态无效的主要(字串[] args)
{
INT X = 3;
的String = Integer.toString(X);
}
作为结束:
公共静态无效的主要(java.lang.String中[]);
码:
0:iconst_3
1:istore_1
2:iload_1
3:invokestatic#2; //方法的Java /郎/ Integer.toString:(I)Ljava /朗/字符串;
6:astore_2
7:返回
虽然这可能只是我的看来,使用的ToString体现你真正想要的东西 - 你想x的字符串值,而用x +是怎样的一个黑客并说 - 我想要使用X连锁与字符串值。
侧面说明:
我不能在中间字节代码C#将发射说话,但我想类似这样的东西。另外,用C#,你可以在你的值类型调用的ToString()一样容易引用类型,所以我觉得我的建议将适用同样的。
Back in college one of my profs. taught us to just do x + ""
as a quick conversion from basic types to strings.
I don't remember which class it was in I had him for some Java and C++ courses(I haven't used either for some time now), but I use it in C#/.Net now which is what I'm primarily developing in lately.
Is there an advantage to using .toString()
over +""
for basic types such at int, decimal, float...?
What cases would .toString()
be better?
Note:I was shown .toString()
as well, that prof just recommended +""
because it was shorter and I have just done that since then without questioning it.
Well, as a side note, it depends on what x is. If x is a primitive in Java, you have to call .toString()
using one of its wrappers, like
Integer.toString(x)
I would say using toString() is generally better, because x + "", in at least Java, is saying you want to append the two Strings together.
Like in this example:
public static void main(String[] args)
{
int x = 3;
String s = x + "";
}
That ends up, in bytecode, as :
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
So it has to create a StringBuilder to append "" and the String value of x together. While the efficiency lost isn't that much, it isn't too much to just use the toString function.
Compare with using toString:
public static void main(String[] args)
{
int x = 3;
String s = Integer.toString(x);
}
Which ends up as:
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
And although it might just be my opinion, using .toString reflects what you actually want -- you want the String value of x, while using x + "" is kind of a hack and says -- I want the String value of x concatenated with "".
Side Note: I can't speak on the intermediate bytecode C# would emit, but I imagine something similar to this. Plus, with C#, you can just call .ToString() on your value types just as easily as reference types, so I think my advice would apply the same.
这篇关于" x.toString()和x + QUOT之间差值;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!