Java的“%n”是什么?在printf? [英] What's up with Java's "%n" in printf?

查看:683
本文介绍了Java的“%n”是什么?在printf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Effective Java,它使用%n 来处理换行符。我已经使用 \ n 而非成功地在Java程序中使用换行符。

I'm reading Effective Java and it uses %n for the newline character everywhere. I have used \n rather successfully for newline in Java programs.

哪个'正确'? \ n 有什么问题?为什么Java改变了这个C约定?

Which is the 'correct' one? What's wrong with \n ? Why did Java change this C convention?

推荐答案

从快速谷歌:


还有一个与参数不对应的说明符。输出换行符的是%n。在某些情况下也可以使用\ n,但由于%n始终输出正确的特定于平台的行分隔符,因此它可以跨平台移植,而\ n则不是。

There is also one specifier that doesn't correspond to an argument. It is "%n" which outputs a line break. A "\n" can also be used in some cases, but since "%n" always outputs the correct platform-specific line separator, it is portable across platforms whereas"\n" is not.

请参阅
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

原始来源

这篇关于Java的“%n”是什么?在printf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆