Java 的“%n"怎么了?在 printf 中? [英] What's up with Java's "%n" in printf?
问题描述
我正在阅读 Effective Java,它在任何地方都使用 %n
作为换行符.我已经成功地将
用于 Java 程序中的换行符.
I'm reading Effective Java and it uses %n
for the newline character everywhere. I have used
rather successfully for newline in Java programs.
哪个是正确"的?
有什么问题?为什么 Java 改变了这个 C 约定?
Which is the 'correct' one? What's wrong with
? Why did Java change this C convention?
推荐答案
来自快速谷歌:
还有一个说明符与参数不对应.输出换行符的是%n".在某些情况下也可以使用 " ",但由于 "%n" 始终输出正确的特定于平台的行分隔符,因此它可以跨平台移植,而 " " 不是.
There is also one specifier that doesn't correspond to an argument. It is "%n" which outputs a line break. A " " can also be used in some cases, but since "%n" always outputs the correct platform-specific line separator, it is portable across platforms whereas" " is not.
请参考https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
这篇关于Java 的“%n"怎么了?在 printf 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!