The difference between print, printf, and println in "Turn" Java

Source: Internet
Author: User

the difference between print, printf, and println in Javaprintf is mainly inherited by the C language of printf Some of the features, can be formatted output

Print is the normal standard output, but does not break the line

println and print basically make no difference, it's the end of the 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 ', the ' x ' integer result is formatted as a hexadecimal integer
' E ', ' e ' floating-point result is formatted as a decimal number in computer science notation
' F ' floating-point result is formatted as a decimal number
' G ', the ' g ' floating point formats the result using computer science notation or decimal format, based on the precision and value of the rounding operation.
' A ', ' a ' floating-point result is formatted as a hexadecimal floating-point number with significant digits and exponent

println ("Test") is equivalent to print ("test\n") is the general output string

The difference 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 newline character at the end, positioning the output cursor at the beginning of the next line.

printf is the form of formatted output.

Below for an example:

Package and 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 ("Output I with println:" + 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

As you can see, with print output I, there is no newline, the result of println output is directly behind the print output statement, and the output println after the line, so with printf output, in the second row

Output.

Let's see printf again.

"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 sequentially

。 Otherwise it would be wrong. And the type should match. If we change the statement to

System.out.printf ("I has a value of%d,j of%f", j,i);//i and J position are 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 shape inconsistency. So it's wrong.

There is also the meaning of "%s" as a point character of a string value. "%c" is the meaning of a character value's dot character.

Perhaps the reader will ask why the J output becomes 5.000000? That is because double is the default has 6 decimal places (this may be related to the system computer, some not 6 bit) but if you want to

Just output two decimal places, okay? Yes, you can! Just change the sentence!

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

Here the "%.2f" means to output two decimal places. If you want to output three bits then "%.3f".

Here you will find that printf is also very useful. This allows you to control the format of the output.

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 has a value of =,j of%.2f", i,j);

}

}

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

The original "%" and "D" between the number of 5 means that the output of 5 placeholders. The default is aligned to the right. Such output is useful, for example, you want to output a table, because the size of the various numbers, some 5 bits have 4 bits, so the output of the table results are not aligned. If the output result is all the same placeholder number. That's all right. Oh.

In addition to "%d" can be so, other can also, readers are not allowed to try. There will be a lot of gains.

Let's try changing the code a bit more:

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 result of the operation is:

The value of I is 00004,

Haha, the original "D" means to output 5 placeholders, if the number is less than 5, there is a 0-phase complement to the left

 

The difference between print, printf, and println in "Turn" 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.