将String作为变量连接到Character vs与其他String连接时有什么区别? [英] What is the difference when concatenating a String as a variable with a Character vs concatenating with an other String?

查看:101
本文介绍了将String作为变量连接到Character vs与其他String连接时有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看到这样的东西(伪1线)时:

When i see something (pseudo 1-liner) like this:

str1 + "a" + str2

是否比以下(伪1线)更差(或更好/相等)?

Is it much worse (or better/equal) than the following (pseudo 1-liner)?

str1 + 'a' + str2

更新:更好的例子(由@QPaysTaxes提供),以减少对原始示例的混淆。

Update: Better example (by @QPaysTaxes) to reduce confusion regarding my original example.

我尝试了什么:
过去10年编程Java的各种东西,但我从来没有真正看到引擎盖下的什么 - 例如我会假设第二个稍微更快/更好,因为没有为斜杠标志创建的String-Object和/或Java的垃圾收集器必须处理更少。
我曾经为Java证书做过准备,并且可能在那段时间内能够更好地争论,但似乎因此我的日常业务中关于Java的理论也必须保持最新状态......我知道没有任何更好的解释,我应该使用 indexOf('c')而不是 indexOf(C)我想知道String-concatenation是否相同。

What i tried: Various stuff for the past 10 years programming Java but i never managed to realy see whats under the hood - e.g. i would assume the second is slightly "faster/better" because there is no String-Object(s) created for the slash-sign and/or the garbage collector of Java has to handle less. I once prepared for the Java Certificates and might would have been able to argue better back in that time but it seems even thus its my daily business the "theory" about Java must be keept up to date as well... I know without any better explanation than my assumptation that indexOf('c') should be used rather than indexOf("C") and i wondered if the same counts for String-concatenation.

我也搜索了一下,但由于我的标题可能暗示我不太好描述我正在寻找的没有例子。对此我感到抱歉,这个障碍的可能性只是产生了重复。

I also googled a bit but as my title might imply i am not quite good to describe what i am looking for without a example. I am sorry for this and the possibility this handicap just produced a duplicate.

我将尝试的内容:
根据此处接受的答案字符串连接:concat()vs" +"运营商我希望能够开始看到幕后的内容,有一天能够争辩/回答这些问题。

What i will try: Based on the accepted answer here String concatenation: concat() vs "+" operator i hope to be able to have a start to see whats under the hood and one day be able to argue/ answer such questions that profund.

推荐答案


根据接受的答案,我希望能够开始
看看引擎盖下的内容。

Based on the accepted answer here I hope to be able to have a start to see whats under the hood.

让我们看一下String与字符连接时生成的字节码:

Let's have a look at the generated bytecode when concatenating a String with a Character:

String str1 = "a" + "test";
String str2 = 'a' + "test";







0: ldc           #2                  // String atest
2: astore_1
3: ldc           #2                  // String atest
5: astore_2

如您所见,没有区别,编译器会将其转换为相同的字节码。

as you can see, there is no difference, the compiler will convert it to the same bytecode.

现在让我们看看在将一个Character连接到一个String变量时生成的字节码。

Now let's have a look at the generated bytecode when concatenating a Character to a String variable.

String str1 = "a" + str3; //str3 is a String
String str2 = 'a' + str3; 







 7: invokespecial #4                  // Method java/lang/StringBuilder."<init>":()V
10: ldc           #5                  // String a
12: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
15: aload_1
16: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
19: invokevirtual #7                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
22: astore_2
23: new           #3                  // class java/lang/StringBuilder
26: dup
27: invokespecial #4                  // Method java/lang/StringBuilder."<init>":()V
30: bipush        97
32: invokevirtual #8                  // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
35: aload_1
36: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
39: invokevirtual #7                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;

正如您所看到的,有一点不同。

As you can see, there is a little difference.

10: ldc           #5                  // String a

ldc 将常量池(String,int或float)中的常量#index推入堆栈。

ldc push a constant #index from a constant pool (String, int or float) onto the stack.

因此,如果你直接与变量连接,连接一个字符会产生更少的字节码,这就是引擎

Therefore, if you are concatenating directly with a variable, concatenating a Character will generate less bytecode, that is what is under the hood.

现在出现性能问题,这不会代表任何显着的性能差异,因为JIT编译器会优化最多的临时对象,除非您在运行程序时指定禁用JIT编译器使用 -Djava.compiler = NONE

Now for the performance issue, this wont represent any signifiant performance difference as the JIT compiler optimize most of the temporary objects, unless you specified when running your program to disable the JIT compiler using -Djava.compiler=NONE.

这篇关于将String作为变量连接到Character vs与其他String连接时有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆