Formatted output of Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.