Understanding of the Java Base-printf () statement

Source: Internet
Author: User

The understanding of print and println is simple, and today suddenly comes into contact with printf (), a little confused, and tidy up and help yourself to a reason

printf is the form of formatted output

Below for an example:

 PackageOther ; Public classTestprint {//DECLARE Print test class Public Static voidMain (string[] args) {//Main methodinti = 4; declaring int Type I
   
Doublej = 5; Declares a double type J System.out.print ("Output I with print:" +i); Compare print (), println (), printf () System.out.println ("Output with println I:" +i);
System.out.printf (
"I has a value of%d,j of%f", i,j); }}

Run the result as

              Print output i:4 with println output i:4

The value of I is 4,j with a value of 5.000000

Focus on the statement:

System.out.printf ("I has a value of%d,j of%f", i,j);

"I has a value of%d,j of%f" and "%d" in this string becomes the value of I, and "%f" becomes the value of J!

here, "%d" means a placeholder for an int value, "%f" is a double or float value, and the parameter of the variable is provided later. Note that the argument names must be sorted in order, and the types should match . otherwise error!

If you change the statement to: System.out.printf ("I has a value of%d,j of%f", j,i); I and J position reversed, Then there is a mistake because the "%d" corresponding parameter changes to J, "%f" corresponds to I, and J is double, and "%d" is an int type of inconsistency. So it's wrong.

In addition:'%s ' is the meaning of a string value's dot character. "%c" is the meaning of a character value's dot character.

 Public class testprint {  publicstaticvoidint i = 4   Double j = 5.000f;
System.out.printf (
The value of "I is%5d,\n", i); The number 5 between% and D means inserting five placeholders \ n characters System.out.printf ("I's value is%3d,j value of%.2f", i,j); The value of J is%.2f meaning that 2 digits after the decimal point are reserved
}}

The result of the operation is:

The value of I is 4,

The value of I is 4,j with a value of 5.00

 Public class testprint {  publicstaticvoid  main (string[] args) {    int   i = 4double j = 5.000f; System.out.printf (The value of "I is%05d,\n", i);      "%05d" means to output 5 placeholders, if the number is less than 5 digits on the left with 0-phase complement}} The result is:i is the value of 00004, 

Summarize

System.out.printf ("I has a value of%d,j of%f", J,i)
%+ (Digital: Placeholder, control output format uniform) + data type (letter shorthand for corresponding data type)  j,i corresponding number and type, strictly on-demand















Understanding of the Java Base-printf () statement

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.