使用String和使用String之间有明显的区别吗?格式和字符串连接在Java?
我倾向于使用String。格式,但偶尔会滑倒和使用连接。我想知道哪个比哪个好。
在我看来,String。Format让你在“格式化”字符串时更强大;连接意味着您不必担心不小心输入了额外的%s或遗漏了一个。
字符串。格式也更短。
哪一个更容易读,取决于你的大脑如何工作。
使用String和使用String之间有明显的区别吗?格式和字符串连接在Java?
我倾向于使用String。格式,但偶尔会滑倒和使用连接。我想知道哪个比哪个好。
在我看来,String。Format让你在“格式化”字符串时更强大;连接意味着您不必担心不小心输入了额外的%s或遗漏了一个。
字符串。格式也更短。
哪一个更容易读,取决于你的大脑如何工作。
当前回答
我想我们可以用MessageFormat。格式,因为它应该在可读性和性能方面都很好。
我使用了与Icaro在上面的回答中使用的相同的程序,并通过添加使用MessageFormat来解释性能数字的代码来增强它。
public static void main(String[] args) {
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("Format = " + ((end - start)) + " millisecond");
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String s = MessageFormat.format("Hi %s; Hi to you %s", i, +i * 2);
}
end = System.currentTimeMillis();
System.out.println("MessageFormat = " + ((end - start)) + " millisecond");
}
串联= 69毫秒 格式= 1435毫秒 MessageFormat = 200毫秒
更新:
根据SonarLint报告,printf风格的格式字符串应该正确使用(squid:S3457)
Because printf-style format strings are interpreted at runtime, rather than validated by the compiler, they can contain errors that result in the wrong strings being created. This rule statically validates the correlation of printf-style format strings to their arguments when calling the format(...) methods of java.util.Formatter, java.lang.String, java.io.PrintStream, MessageFormat, and java.io.PrintWriter classes and the printf(...) methods of java.io.PrintStream or java.io.PrintWriter classes.
我用括号替换了printf样式,得到了一些有趣的结果,如下所示。
串联= 69毫秒格式= 1107毫秒 格式:括号= 416毫秒MessageFormat = 215 毫秒MessageFormat:括号= 2517毫秒
我的结论是: 如上所述,使用String。带尖括号的格式应该是一个很好的选择,以获得良好的可读性和性能。
其他回答
我建议使用String.format()是更好的实践。主要原因是String.format()可以更容易地使用从资源文件加载的文本进行本地化,而拼接如果不为每种语言生成带有不同代码的新可执行文件就无法进行本地化。
如果你打算让你的应用程序本地化,你也应该养成为你的格式标记指定参数位置的习惯:
"Hello %1$s the time is %2$t"
然后可以对其进行本地化,并交换名称和时间令牌,而不需要重新编译可执行文件以考虑不同的顺序。对于参数位置,你也可以重复使用相同的参数,而不需要将其传递给函数两次:
String.format("Hello %1$s, your name is %1$s and the time is %2$t", name, time)
我想我们可以用MessageFormat。格式,因为它应该在可读性和性能方面都很好。
我使用了与Icaro在上面的回答中使用的相同的程序,并通过添加使用MessageFormat来解释性能数字的代码来增强它。
public static void main(String[] args) {
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("Format = " + ((end - start)) + " millisecond");
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
String s = MessageFormat.format("Hi %s; Hi to you %s", i, +i * 2);
}
end = System.currentTimeMillis();
System.out.println("MessageFormat = " + ((end - start)) + " millisecond");
}
串联= 69毫秒 格式= 1435毫秒 MessageFormat = 200毫秒
更新:
根据SonarLint报告,printf风格的格式字符串应该正确使用(squid:S3457)
Because printf-style format strings are interpreted at runtime, rather than validated by the compiler, they can contain errors that result in the wrong strings being created. This rule statically validates the correlation of printf-style format strings to their arguments when calling the format(...) methods of java.util.Formatter, java.lang.String, java.io.PrintStream, MessageFormat, and java.io.PrintWriter classes and the printf(...) methods of java.io.PrintStream or java.io.PrintWriter classes.
我用括号替换了printf样式,得到了一些有趣的结果,如下所示。
串联= 69毫秒格式= 1107毫秒 格式:括号= 416毫秒MessageFormat = 215 毫秒MessageFormat:括号= 2517毫秒
我的结论是: 如上所述,使用String。带尖括号的格式应该是一个很好的选择,以获得良好的可读性和性能。
下面是与上面相同的测试,只是修改了对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
你不能比较字符串连接和字符串。格式由上面的程序。
你也可以尝试交换使用你的字符串的位置。格式和连接在你的代码块如下所示
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
for( int i=0;i<1000000; i++){
String s = String.format( "Hi %s; Hi to you %s",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") ;
}
您会惊讶地发现,Format在这里工作得更快。这是因为创建的初始对象可能不会被释放,内存分配可能会出现问题,从而影响性能。
.format的一个问题是失去了静态类型安全性。格式的参数可能过少,格式说明符的类型也可能错误——这两种情况都会导致运行时出现IllegalFormatException异常,因此可能会生成破坏生产的日志代码。
相反,+的参数可以由编译器测试。
printf的安全历史(格式化函数是在它的基础上建模的)是漫长而可怕的。