Does concatenating strings in Java always lead to new strings being created in memory?(在 Java 中连接字符串是否总是会导致在内存中创建新字符串?)
问题描述
我有一个不适合屏幕宽度的长字符串.例如.
I have a long string that doesn't fit the width of the screen. For eg.
String longString = "This string is very long. It does not fit the width of the screen. So you have to scroll horizontally to read the whole string. This is very inconvenient indeed.";
为了方便阅读,我想到了这样写——
To make it easier to read, I thought of writing it this way -
String longString = "This string is very long." +
"It does not fit the width of the screen." +
"So you have to scroll horizontally" +
"to read the whole string." +
"This is very inconvenient indeed.";
但是,我意识到第二种方法使用字符串连接,会在内存中创建 5 个新字符串,这可能会导致性能下降.是这样吗?或者编译器是否足够聪明,可以找出我真正需要的只是一个字符串?我怎么能避免这样做呢?
However, I realized that the second way uses string concatenation and will create 5 new strings in memory and this might lead to a performance hit. Is this the case? Or would the compiler be smart enough to figure out that all I need is really a single string? How could I avoid doing this?
推荐答案
我意识到第二种方式使用字符串连接,会在内存中创建 5 个新字符串,这可能会导致性能下降.
I realized that the second way uses string concatenation and will create 5 new strings in memory and this might lead to a performance hit.
不,不会的.由于这些是字符串文字,它们将在编译时进行评估,并且只会创建一个字符串.这是在 Java 语言中定义的规范#3.10.5:
No it won't. Since these are string literals, they will be evaluated at compile time and only one string will be created. This is defined in the Java Language Specification #3.10.5:
一个长字符串总是可以被分解成更短的部分,并使用字符串连接运算符 +
写成一个(可能带括号的)表达式[...]
此外,字符串字面量总是引用 String 类的同一个实例.
A long string literal can always be broken up into shorter pieces and written as a (possibly parenthesized) expression using the string concatenation operator +
[...]
Moreover, a string literal always refers to the same instance of class String.
- 由常量表达式(第 15.28 节)计算的字符串在编译时计算,然后将其视为文字.
- 在运行时通过串联计算的字符串是新创建的,因此是不同的.
测试:
public static void main(String[] args) throws Exception {
String longString = "This string is very long.";
String other = "This string" + " is " + "very long.";
System.out.println(longString == other); //prints true
}
但是,下面的情况情况不同,因为它使用了一个变量——现在有一个连接,并且创建了几个字符串:
However, the situation situation below is different, because it uses a variable - now there is a concatenation and several strings are created:
public static void main(String[] args) throws Exception {
String longString = "This string is very long.";
String is = " is ";
String other = "This string" + is + "very long.";
System.out.println(longString == other); //prints false
}
这篇关于在 Java 中连接字符串是否总是会导致在内存中创建新字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中连接字符串是否总是会导致在内存中创建新字符串?
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01