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%5d,\n", i);
System.out.printf ("I has a value of%3d,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%05d,\n", i);
}
}
The result of the operation is:
The value of I is 00004,
Haha, the original "%05d" 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\println\printf