Does Java Compiler include String Constant Folding?(Java 编译器是否包含字符串常量折叠?)
问题描述
我发现 Java 支持原始类型的常量折叠,但是 Strings 呢?
I found out that Java supports constant folding of primitive types, but what about Strings?
示例
如果我创建以下源代码
out.write(""
+ "<markup>"
+ "<nested>"
+ "Easier to read if it is split into multiple lines"
+ "</nested>"
+ "</markup>"
+ "");
编译后的代码中有什么内容?
What goes into the compiled code?
组合版本?out.write("<markup><nested>如果分成多行更容易阅读</nested></markup>");
还是效率较低的运行时串联版本?out.write(new StringBuilder("").append("<markup>").append("<nested>").append("拆分成多行更容易阅读").append("</nested>").append("</markup>").append(""));
Or the less efficient run-time concatenation version? 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
所以,是的,编译器会折叠.
So, yes, the compiler will fold.
这篇关于Java 编译器是否包含字符串常量折叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 编译器是否包含字符串常量折叠?
基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
