A detailed explanation of the differences between print, printf, and println in Javaprintf 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 ("test\n") is a generic output string
The difference between the print\println\printf
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:
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" 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, 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 is%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 with 6 decimal digits (this may be related to system computers, some are not 6) but if you want to
Just output two decimal places is not OK. Yes, I can. Just change the statement.
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:
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:
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