Just came to know that java
does has a method named printf
, then what is the difference between printf
& println
?
println is used when you want to create a new line with just simple text or even text containing concatenation. ("Hello " + "World" = "Hello World.") printf is used when you want to format your string. This will clean up any concatenation.
The main difference between print and println is that print method prints the string but does not move the cursor to a new line while println method prints the string and moves the cursor to a new line.
Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places. /* Code example to print a double to two decimal places with Java printf */ System.
printf: prints the formatted String into console much like System. out. println() but. format: method return a formatted String, which you can store or use the way you want.
System.out.println();
is efficient for simply printing a line of text. If the line of text needs to be formatted (ex: alignment (left-justified, etc.), etc.), then System.out.printf();
would be used.
Check out this link for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With