Analysis of the difference between print, printf and println in Java _java

Source: Internet
Author: User

printf mainly inherits some of the features of the C language printf, which can be formatted for output
Print is the normal standard output, but does not wrap.
println and print basically make no difference, is the last line
System.out.printf ("The number Is:d", t);

The reference Java API is defined as follows:
The ' d ' integer result is formatted as a decimal integer
The ' o ' integer result is formatted as an octal integer
' x ', ' x ' integer result is formatted as a hexadecimal integer
The ' e ', ' e ' floating-point result is formatted as a decimal number in the computer science notation notation
The ' F ' floating-point result is formatted as a decimal number
' G ', ' g ' floating-point formats the results using computer science notation or decimal format, based on precision and rounding values.
' A ', ' a ' floating-point result is formatted as a hexadecimal floating-point number with valid digits and indices
println ("Test") is equivalent to print ("Testn") is a generic output string

The difference between the printprintlnprintf
Print displays its arguments in the command window and positions the output cursor after the last character displayed.
PRINTLN displays its arguments in the command window and ends with a newline character, positioning the output cursor at the beginning of the next line.
printf is the form of formatted output.

Here's an example:

Copy Code code as follows:

package other;
public class Testprint {
public static void Main (string[] args) {
int i = 4;
Double J = 5;

System.out.print ("With print output I:" + i);
System.out.println ("With println output I:" + i);
The value of System.out.printf ("I is the value of%d,j is%f", i,j);

}
}

Run result is
Print output i:4 with println output i:4
The value of I is 4,j to 5.000000

You can see that with the print output I, there is no line, with the results of println output directly behind the print output statement, and output println after the line, so with printf output, in the second line
Output.

Let's see printf again.
"I has a value of%d,j is%f" the "%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, and the parameter of the variable is provided in the following order. Note that the argument names must be sorted in order. Otherwise it would be wrong. And the type should match. If we change the statement to System.out.printf (the value of "I%d,j is%f", j,i);//i and J are in reverse.
And then there's a mistake. Because "%d" corresponds to the parameter to J, "%f" corresponds to I, and J is double, and "%d" is an int form inconsistency. So it's wrong.
and "%s" is meant to be a bit character of a string value. "%c" is the meaning of a character value's point-bit character.
Perhaps the reader will also ask why the J output has become 5.000000? That's because double is the default of 6 decimal places (this may be related to the system computer, and some are not 6) but if you want to output two decimal places? Yes, you can! Just change the statement on the line!
The value of System.out.printf ("I is the value of%d,j is%.2f", i,j);
The "%.2f" here means the output of two decimal places. If you want to output three digits, then "%.3f".
Speaking of which, you'll find that printf is also useful. This allows you to control the format of the output.
Learn more knowledge, and change the code to the following:

Copy Code code as follows:

public class Testprint {
public static void Main (string[] args) {
int i = 4;
Double J = 5.000f;
SYSTEM.OUT.PRINTF (the value of "I is],n", i);
The value of System.out.printf ("I is the value of =,j is%.2f", i,j);
}
}

the results of the operation are:
The value of I is 4,
The value of I is 4,j to 5.00

The original number 5 between "%" and "D" means that the output is 5 placeholders. Aligns to the right by default. Such output is useful, such as you want to output a table, because the number of different sizes, some 5-bit 4-bit, so that the output of the table results are not aligned. If the output is all the same as the placeholder number. That's right. Oh.
In addition to "%d" can be so, the other can, the reader may not try. There will be a lot of gains.

Let's try changing the code again:

Copy Code code as follows:

public class Testprint {
public static void Main (string[] args) {
int i = 4;
Double J = 5.000f;
SYSTEM.OUT.PRINTF (the value of "I is d,n", i);
}
}

the results of the operation are:
The value of I is 00004,
Haha, the original "D" means that the output of 5 placeholders, if the number is less than 5, there is a left to 0 complement

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.