两个Java字符串的串联 [英] Concatenation of two Java strings

查看:76
本文介绍了两个Java字符串的串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 当我编写以下代码时

  1. When I write the following code

int a = "Java";

java编译器显示以下错误:

the java compiler displays the following error :

无法从String转换为int

cannot convert from String to int

因此常量 Java 被视为从
String 类派生的对象。

So the constant Java is considered as an object derived from String class.

-是否确实将 之间包含的每个字符列表都视为从String类派生的对象?

- Is it true that each list of characters contained between "" is considered as an object derived from String class?

当我尝试连接两个字符串时,我使用了以下两个
例子:

When I try to have concatenation of two strings I used the two following examples:

String ch1 = "Java " ;
String ch2 = "is cool";
String ch3 = ch1 + ch2;

在我使用命令 javap -c 之后在我的已编译类中,我发现
是编译器使用 StringBuilder 的实例将
附加两个字符串。

after I used the command javap -c on my compiled class I found that the compiler use an instance of StringBuilder to append the two strings.

,但还有另一个示例:

String ch = "Java " + "is cool"; 

尽管两个常量 Java和很酷都是从<$ c派生的两个对象$ c> String
类,编译器不使用 StringBuilder 的实例向
追加两个字符串。

Although the two constants "Java " and "is cool" are both two objects derived from String class, the compiler doesn't use an instance of StringBuilder to append the two strings.

-那么第二个示例中使用的方法是什么?

- So what's the approach used in the second example?


推荐答案

从这里开始。


'+'每次连接时都会创建一个新的String对象

'+' creates a new String object every time it concatenates something, except when the concatenation is done at compile time.

虽然这不是一个有信誉的来源,但从我记得我的教科书中可以看出来这听起来很正确。因此,基本上,将其应用于您的代码:

While this is not a reputable source, from what I remember from my textbooks this sounds correct. So basically, applying this to your code:

String ch = "Java " + "is cool";

由于已经定义了两个常量并将它们串联在一起,因此将在编译时进行处理,这意味着结果实际上也是一个常数,因此可以这样处理并在编译时进行计算。看看是否先编译该代码然后反编译以查看该语句的读取方式会很有趣,我想它可能会显示为:

Would be handled at compile time since you've defined two constants and concatenated them together, which implies that the result is also in fact a constant and thus can be treated as such and calculated at compile time. It would be interesting to see if you compiled that code then decompiled to see how that statement would read, I'd imagine it may read:

String ch = "Java is cool";

至于另一条语句:

String ch1 = "Java " ;
String ch2 = "is cool";
String ch3 = ch1 + ch2;

由于ch3是根据ch1和ch2计算的,因此在运行时完成,因为ch1和ch2是变量

Since ch3 is calculated from ch1 and ch2, it is done at runtime since ch1 and ch2 are variables instead of constants.

对于您的第一个问题,我找不到确切的引用,但是从我记得的印象中,表示字符串,就像暗示一个角色。我不确定您要使用该语句做什么,但我可以想象您可以将字符串转换为char数组,然后将其转换为int数组。

As for your first question, I can't find any references exactly, but from what I remember yes the "" implies a string, just like '' implies a character. I'm not exactly sure what you're trying to do with that statement, but I would imagine you could convert your string into a char array and then cast it to an int array.

这篇关于两个Java字符串的串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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