gpt4 book ai didi

java - Java 编译器是否包含字符串常量折叠?

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:55 24 4
gpt4 key购买 nike

我发现 Java supports constant folding of primitive types ,但是 String 呢?是吗?

示例

如果我创建以下源代码

out.write(""
+ "<markup>"
+ "<nested>"
+ "Easier to read if it is split into multiple lines"
+ "</nested>"
+ "</markup>"
+ "");

编译代码中包含什么?

合并版本? out.write("<markup><nested>Easier to read if it is split into multiple lines</nested></markup>");

还是效率较低的运行时串联版本? out.write(new StringBuilder("").append("<markup>").append("<nested>").append("Easier to read if it is split into multiple lines").append("</nested>").append("</markup>").append(""));

最佳答案

这是一个简单的测试:

public static void main(final String[] args) {
final String a = "1" + "2";
final String b = "12";

System.out.println(a == b);
}

输出:

true

所以,是的,编译器会折叠。

关于java - Java 编译器是否包含字符串常量折叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8581824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com