使用String和使用String之间有明显的区别吗?格式和字符串连接在Java?
我倾向于使用String。格式,但偶尔会滑倒和使用连接。我想知道哪个比哪个好。
在我看来,String。Format让你在“格式化”字符串时更强大;连接意味着您不必担心不小心输入了额外的%s或遗漏了一个。
字符串。格式也更短。
哪一个更容易读,取决于你的大脑如何工作。
使用String和使用String之间有明显的区别吗?格式和字符串连接在Java?
我倾向于使用String。格式,但偶尔会滑倒和使用连接。我想知道哪个比哪个好。
在我看来,String。Format让你在“格式化”字符串时更强大;连接意味着您不必担心不小心输入了额外的%s或遗漏了一个。
字符串。格式也更短。
哪一个更容易读,取决于你的大脑如何工作。
当前回答
一般来说,字符串连接应该优先于string .format。后者有两个主要缺点:
它不以本地方式对要构建的字符串进行编码。 构建过程编码在字符串中。
第一点,我的意思是不可能理解String.format()调用在一次连续传递中所做的事情。人们被迫在格式字符串和参数之间来回切换,同时计算参数的位置。对于短连接,这不是什么大问题。但是,在这些情况下,字符串连接不那么详细。
第2点,我的意思是构建过程的重要部分编码在格式字符串中(使用DSL)。使用字符串表示代码有很多缺点。它本身不是类型安全的,并且使语法高亮显示、代码分析和优化等变得复杂。
当然,在使用Java语言之外的工具或框架时,可能会出现新的因素。
其他回答
错误的测试重复多次 不应该使用{}%s。
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String s = "Hi " + i + "; Hi to you " + i * 2;
}
long end = System.currentTimeMillis();
System.out.println("Concatenation = " + ((end - start)) + " millisecond");
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String s = String.format("Hi %s; Hi to you %s", i, +i * 2);
}
end = System.currentTimeMillis();
System.out.println("Wrong use of the message format = " + ((end - start)) + " millisecond");
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String s = String.format("Hi {0}; Hi to you {1}", i, +i * 2);
}
end = System.currentTimeMillis();
System.out.println("Good use of the message format = " + ((end - start)) + " millisecond");
}
Concatenation = 88 millisecond
Wrong use of the message format = 1075 millisecond
Good use of the message format = 376 millisecond
.format的一个问题是失去了静态类型安全性。格式的参数可能过少,格式说明符的类型也可能错误——这两种情况都会导致运行时出现IllegalFormatException异常,因此可能会生成破坏生产的日志代码。
相反,+的参数可以由编译器测试。
printf的安全历史(格式化函数是在它的基础上建模的)是漫长而可怕的。
It takes a little time to get used to String.Format, but it's worth it in most cases. In the world of NRA (never repeat anything) it's extremely useful to keep your tokenized messages (logging or user) in a Constant library (I prefer what amounts to a static class) and call them as necessary with String.Format regardless of whether you are localizing or not. Trying to use such a library with a concatenation method is harder to read, troubleshoot, proofread, and manage with any any approach that requires concatenation. Replacement is an option, but I doubt it's performant. After years of use, my biggest problem with String.Format is the length of the call is inconveniently long when I'm passing it into another function (like Msg), but that's easy to get around with a custom function to serve as an alias.
下面是与上面相同的测试,只是修改了对StringBuilder调用toString()方法。下面的结果表明,StringBuilder方法比使用+操作符的字符串连接稍微慢一点。
文件:StringTest.java
class StringTest {
public static void main(String[] args) {
String formatString = "Hi %s; Hi to you %s";
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String s = String.format(formatString, i, +i * 2);
}
long end = System.currentTimeMillis();
System.out.println("Format = " + ((end - start)) + " millisecond");
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String s = "Hi " + i + "; Hi to you " + i * 2;
}
end = System.currentTimeMillis();
System.out.println("Concatenation = " + ((end - start)) + " millisecond");
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
StringBuilder bldString = new StringBuilder("Hi ");
bldString.append(i).append("Hi to you ").append(i * 2).toString();
}
end = System.currentTimeMillis();
System.out.println("String Builder = " + ((end - start)) + " millisecond");
}
}
Shell命令:(编译并运行StringTest 5次)
> javac StringTest.java
> sh -c "for i in \$(seq 1 5); do echo \"Run \${i}\"; java StringTest; done"
结果:
Run 1
Format = 1290 millisecond
Concatenation = 115 millisecond
String Builder = 130 millisecond
Run 2
Format = 1265 millisecond
Concatenation = 114 millisecond
String Builder = 126 millisecond
Run 3
Format = 1303 millisecond
Concatenation = 114 millisecond
String Builder = 127 millisecond
Run 4
Format = 1297 millisecond
Concatenation = 114 millisecond
String Builder = 127 millisecond
Run 5
Format = 1270 millisecond
Concatenation = 114 millisecond
String Builder = 126 millisecond
我没有做任何具体的基准测试,但我认为连接可能更快。String.format()创建一个新的Formatter,该Formatter反过来创建一个新的StringBuilder(大小只有16个字符)。这是一个相当大的开销,特别是当您正在格式化一个较长的字符串并且StringBuilder必须不断调整大小时。
然而,串联不太有用,也更难阅读。一如既往,有必要对代码进行基准测试,看看哪个更好。在服务器应用程序中,当你的资源包、locale等加载到内存中,代码被jit后,这些差异可以忽略不计。
也许作为一个最佳实践,它将是一个好主意,创建自己的Formatter与适当大小的StringBuilder(可追加的)和地区,并使用它,如果你有很多格式化要做。