Reference Http://how2j.cn/k/number-string/number-string-foramt/320.html#nowhere
Formatted output
If you do not use formatted output, you need to do string concatenation, if the variable is more, stitching will appear cumbersome
Using formatted output, you can simply and concisely
%s represents a string
%d indicates a number
%n indicates line break
Using System.out.printf
Package Digit; Public classTestnumber { Public Static voidMain (string[] args) {String name="Galen"; intKill =8; String title="Super God"; //Direct use + string connection, coding feeling is more cumbersome, and poor maintenance, easy to read poorString sentence = name+"in a continuous"+ Kill +"after the killing, the"+ title +"the title"; System. out. println (sentence); //using formatted output//%s represents a string,%d represents a number, and%n represents a newlineString Sentenceformat ="%s was awarded the title of%s after a series of%d kills%n"; System. out. printf (sentenceformat,name,kill,title); }}
printf and format
The source code of printf
Package Digit; Public classTestnumber { Public Static voidMain (string[] args) {String name="Galen"; intKill =8; String title="Super God"; String Sentenceformat="%s was awarded the title of%s after a series of%d kills%n"; //format output with printfSystem. out. printf (sentenceformat,name,kill,title); //format output using formatSystem. out. Format (sentenceformat,name,kill,title); }}
Line break
is another line---' \ n ' newline (newline)
carriage return is the beginning of the line---' \ r ' carriage return
knocking a carriage return in eclipse is actually the Span class= "Strong" > carriage return newline
Java is a cross-platform programming language, and the same code can be used on different platforms, such as Windows,linux , Mac
However, in different operating systems, newline characters are not the same
(1) in DOS and Windows, each line ends in "\ r \ n";
(2) Linux system, each line ends only "\ n";
(3) in MAC system, each line ends with "\ R ".
in order to make the same Java program line break in all operating systems have the same performance, using%n, you can do platform-independent line wrapping
package digit; Public class Testnumber { publicstaticvoid main (string[] args) { System. out. printf (" This is the newline character%n"); System. out. printf (" This is a newline character%n");} }
Formatted output of Java