Similarities and differences between print, printf, and println functions in Java

Source: Internet
Author: User

Printf inherits some features of the C language printf and can be formatted and output.

Print is the standard output, but does not wrap

There is basically no difference between println and print, that is, the last line break.

System. out. printf ("the number is: d", t );
The definition of java api is as follows:
The 'D' integer is formatted as a decimal integer.
The 'O' integer is formatted as an octal integer.
'X', 'X' integer result is formatted as a hexadecimal integer
'E', 'E' floating point result is formatted as a decimal number expressed in the computer science notation
'F' the floating point result is formatted as a decimal number.
'G' and 'G' floating point are formatted in computer scientific notation or decimal format based on the value after precision and rounding.
'A', 'A' floating point result is formatted as a hexadecimal floating point number with valid digits and indexes

Println ("test") is equivalent to print ("test \ n").

Differences between print \ println \ printf

Print displays its parameters in the command window, and positions the output cursor after the last character displayed.

Println displays its parameters in the command window, and adds a line break at the end to position the output cursor at the beginning of the next line.

Printf is the form of formatting output.

The following is an example:

Package other; public class TestPrint {public static void main (String [] args) {int I = 4; double j = 5; System. out. print ("output with print I:" + I); System. out. println ("output I with println:" + I); System. out. printf ("The value of I is % d, and the value of j is % f", I, j );}}



The running result is

Output I: 4 with print output I: 4 with println

The I value is 4, and the j value is 5.000000.

We can see that there is no line feed after I is output with print, and the result output with println is directly behind the print output statement, while the output with println is wrapped. Therefore, when printf is used for output, there is a line break in the second line.

Output.

Let's take a look at printf.

The value of "I" is % d, and the value of j is % f. "% d" in this string is changed to the value of I, and "% f" is changed to the value of j!

Here, "% d" means a placeholder for an int value. "% f" is a point character of a double or float value. The parameter of this variable is provided later. Note that real parameter names must be ranked in order

. Otherwise, it will be wrong. And the type must be consistent. If we change the statement

System. out. printf ("The value of I is % d, and the value of j is % f", j, I); // I and j are reversed.

In this case, the error occurs because the parameter corresponding to "% d" is changed to j, "% f" is changed to I, and j is double, it is inconsistent with "% d" in int form. So there is something wrong.

Also, "% s" indicates the point character of a string value. "% C" indicates the point character of a character value.

Readers may also ask why the j output is 5.000000? That's because double has 6 decimal places by default (this may be related to the system computer, but some are not 6 digits). But if you want

Can I output only two decimal rows? Yes! Just change the statement!

System. out. printf ("The I value is % d, and the j value is %. 2f", I, j );

"%. 2f" indicates that two decimal places are output. If you want to output three digits, "%. 3f ".

Speaking of this, you will find that the original printf is also very useful. In this way, the output format can be controlled.

Learn more and change the code to the following:

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); System. out. printf ("I value =, j value %. 2f ", I, j );}}



The running result is:

The I value is 4,

The I value is 4, and the j value is 5.00.

Adding 5 numbers between "%" and "d" means to output 5 placeholders. It is aligned to the right by default. This output is very useful. For example, if you want to output a table, because the numbers are different in size, some 5 bits and 4 bits, the output table results will be different. If all the output results are the same number of placeholders. Then it is aligned. Haha.

In addition to "% d", you can also try it. There will be a lot of gains.

Let's try again and change the code:

Public class TestPrint {public static void main (String [] args) {int I = 4; double j = 5.000f; System. out. printf ("I value: d, \ n", I );}}



The running result is:

The I value is 00004,

Haha, the original "d" means to output 5 placeholders. If the number is less than 5, there will be 0 on the left side.



Next, let's take a look at the different examples of print and println in java.

For a small difference in print and println output, I found relevant information on the Internet and explained that the print () method will end the operation after the string specified in the output brackets, instead of adding a carriage return, the cursor stays at the right of the last character of the string, and println () is to add a carriage return, and the cursor stops at the next line.
Record it here.
When using print to compile

Public class ForTest {public static void main (String agrs []) {for (int I = 1; I <100; I ++) {if (I % 7 = 0) system. out. print (I + "");}}}



Running result: 7 14 21 28 35 42 49 56 63 70 77 84 91 98
However, when println is used for compilation

Public class ForTest {public static void main (String agrs []) {for (int I = 1; I <100; I ++) {if (I % 7 = 0) system. out. println (I + "");}}}


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.