A brief analysis of the differences between print printf and println in Java

  • 2020-04-01 02:08:46
  • OfStack

Printf mainly inherits some features of printf in C language and can format output
Print is normal standard output, but does not wrap
Println is basically the same thing as print, except that at the end of the line, okay
System. The out. Printf (" the number is: d ", t);

Refer to the definition of JAVA API as follows:
The 'd' integer result is formatted as a decimal integer
The 'o' integer result is formatted as an octal integer
The 'x', 'x' integer results are formatted as hexadecimal integers
The 'e', 'e' floating point results are formatted as decimal Numbers in computer science notation
The 'f' floating point result is formatted as a decimal number
The 'g', 'g' floating point formats the results in computer science notation or decimal format, based on the precision and rounded values.
'a', 'a' floating point results are formatted as hexadecimal floating point Numbers with significant digits and exponents
Println ("test") is the same thing as print("testn"), which is the normal output string

The difference between 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 adds a newline at the end to position the output cursor at the beginning of the next line.
Printf is the form that formats the output.

Here is an example:


package other;
public class TestPrint {
public static void main(String[] args) {
   int i = 4;
   double j = 5;

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

}
}

The running result is
Print out I :4 and print out I :4 with println
The value of I is 4, and the value of j is 5.000000

As you can see, after printing I with print, there is no newline, the output with println is directly after the print output statement, and the output with println is newline, so when you print with printf, it's on the second line
The output.

So let's look at printf
The "%d" in the string "I value %d,j value %f" becomes the value of I, and "%f" becomes the value of j!
Here, "%d" means a placeholder for an int value, and "%f" means a dot character for a double or float value. Note that argument names must be in order. Otherwise it would be wrong. And the type should match. If we change the statement to system.out.printf (" I value %d,j value %f", j, I); // I and j are reversed
And then you have an error because "%d" is going to be j, "%f" is going to be I, and j is going to be double, which is different from "%d" being int. So there's a mistake.
Also "%s" is the point character of a string value. %c" is the dot character meaning of a character value.
Maybe the reader will 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 of them are not 6 decimal places). B: yes, you can! Just change the statement!
System.out.printf(" I value %d,j value %.2f", I,j);
"%.2f" here means output two decimal points. If you want to output three bits, "%.3f".
So you'll see that printf is also useful. This allows you to control the format of the output.
To learn more, 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("i The value of ],n", i);
   System.out.printf("i The value of =,j The value of %.2f", i,j);
}
}

The running result is:
The value of I is & PI;     4,
The value of I is 4, and the value of j is 5.00

Adding the number 5 between "%" and "d" originally meant printing out five placeholders. Align to the right by default. This output is very useful, for example, if you want to output a table, because the size of each number is different, some 5 bits, some 4 bits, so the output table results are not aligned. If the output is all the same placeholder number. So it's aligned. Ha ha.
Except "%d" can do this, other can also, the reader does not want to try. There will be a lot to gain.

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("i The value of d,n", i);
}
}

The running result is:
The value of I is 00004,
Haha, the original meaning of "d" is to output 5 placeholders. If the number is less than 5, the left hand side is complemented by 0


Related articles: