Blog


What’s up with Java’s “%n” in printf?

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

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

 

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

Here is a basic example:


int i = 461012;
System.out.format("The value of i is: %d%n", i);

The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is:

The value of i is: 461012
The printf and format methods are overloaded. Each has a version with the following syntax:


public PrintStream format(Locale l, String format, Object... args)




道德经

道可道,非常道。名可名,非常名。无,名天地之始。有,名万物之母。故常无,欲以观其妙。常有,欲以观其。此两者同出而异名,同谓之玄。玄之又玄,众妙之门。




逍遥游

北冥有鱼,其名为鲲。鲲之大,不知其几千里也。化而为鸟,其名 为鹏。鹏之背,不知其几千里也。怒而飞,其翼若垂天之云。是鸟也 ,海运则将徙于南冥。南冥者,天池也。

《齐谐》者,志怪者也。《谐》之言曰:“鹏之徙于南冥也,水击 三千里,抟扶摇而上者九万里,去以六月息者也。”野马也,尘埃也 ,生物之以息相吹也。天之苍苍,其正色邪?其远而无所至极邪?其 视下也,亦若是则已矣。