The printf method can be particularly useful when displaying multiple variables in one line which would be tedious using string concatenation:
The println() which can be confusing at times. (Although both can be used in almost all cases).

 double a = 10;
 double b = 20;

System.out.println("a: " + a + " b: " + b);// Tedious string concatenation.

System.out.printf("a: %f b: %f\n", a, b);// Output using string formatting.

Output:

a: 10.0 b: 20.0
a: 10,000000 b: 20,000000
Answer from Abdelhak on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_output_printf.asp
Java Output printf() Method
The %s character is a placeholder for the string "World": ... Note: You will find more "Try it Yourself" examples at the bottom of this page. The printf() method outputs a formatted string.
🌐
Oxford University
mathcenter.oxford.emory.edu › site › cs170 › printf
The printf Method
double x = 12.345678; System.out.printf("Number is approximately %.2f", x); //prints "Number is approximately 12.35" System.out.printf( format, item1, item2, ..., itemK ); Here, "format" is a string to print with "format specifiers" (each consisting of a percent sign and a conversion code) ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Format-double-Java-printf-example
How to format a Java double with printf example
The %f specifier works with both double and float values. The decimal and number after the % specify the number of decimals to print. A comma after the % sign adds a grouping separator for large numbers.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-to-use-Java-printf-to-format-output
Format output with Java printf
The .3 instructs Java printf to limit output to three decimal places. %f is the specifier used to format double and floats with printf.
Top answer
1 of 2
10

The printf method can be particularly useful when displaying multiple variables in one line which would be tedious using string concatenation:
The println() which can be confusing at times. (Although both can be used in almost all cases).

 double a = 10;
 double b = 20;

System.out.println("a: " + a + " b: " + b);// Tedious string concatenation.

System.out.printf("a: %f b: %f\n", a, b);// Output using string formatting.

Output:

a: 10.0 b: 20.0
a: 10,000000 b: 20,000000
2 of 2
3

You can do both (if System.out.println("Your number was " + median); is not working you have something else wrong. Show us the stack trace). printf allows you some more formatting options (such as the number of decimal places) without having to use a DecimalFormat, but it is a choice on style rather than anything else.

Personally I find string concatenation easer on the eyes and easer to maintain. The %blah gets lost in the string, errors in string concatenation is a compile time error and adding a new variable requires less thought. However I'm in the minority on this.

The pros in printf is that you don't need to construct a bunch of formatters, others find its format easer on the eyes and when using a printf like method provided by a logging framework the string building only happens if actually required.

double value = 0.5;
System.out.println("value: " + value);
System.out.printf("value: %f", value);

If you want to set a number of decimal places:

    double value = 0.5;
    // String concatenation 
    DecimalFormat df = new DecimalFormat("0.000");
    System.out.println("value: " + df.format(value));

    // printf
    System.out.printf("value: %.3f", value);
🌐
Learningkoala
learningkoala.com › java-printf.html
Java - System.out.printf - LearningKoala
January 2, 2026 - Here are some examples how to use the printf() method: package com.learningkoala; import java.util.Date; public class MyFormattedOutput { public static void main(String[] args) { double x = 100000.0 / 3.0; // Unix timestamp (number of milliseconds since January 1, 1970) long exampleTime = 1711638903488L; Date currentDate = new Date(); System.out.println("The default value of x is: " + x); // %.2f%n means format as floating point with 2 decimal places, %n is a new line System.out.printf("Variant 1 - The formatted value of x is: %.2f%n", x); // %, .2f%n - include grouping separator and format as
Find elsewhere
🌐
xperti
xperti.io › home › formatting with printf() in java
Guide To Use The Java printf() Method For Formatting
May 17, 2022 - We discussed how to use the printf method in Java to format date/time, strings, numbers, and boolean values to be printed. There is a list of possible options made available in Java so that developers can easily format the output based on their needs and requirements of the program.
🌐
Colorado State University
cs.colostate.edu › ~cs160 › .Spring16 › resources › Java_printf_method_quick_reference.pdf pdf
Colostate
CS 160 Foundations in Programming · Spring 2016 Computer Science Department CS 160 Homepage
🌐
W3Schools
w3schools.com › c › c_variables_format.php
C Format Specifiers
A format specifier always starts with a percentage sign %, followed by a letter. For example, to output the value of an int variable, use the format specifier %d surrounded by double quotes (""), inside the printf() function:
🌐
Quarkphysics
quarkphysics.ca › ICS3U1 › unit1 › print.htm
1.6 Intro to Java: printf - QuarkPhysics.ca
The way to print something to the ... the line. Thus System.out.print("text\n") is the same as System.out.println("text") When you print a double Java will display at least one decimal place (eg....
🌐
TutorialsPoint
tutorialspoint.com › article › format-double-type-in-java
Format double type in Java
March 11, 2026 - System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1)); System.out.printf("log = %.3f%n", val1, Math.log(val1)); The following is an example where we have also shown other ways to format double in Java.
🌐
W3Schools
w3schools.com › c › ref_stdio_printf.php
C stdio printf() Function
L - Expect long double type for floating point numbers. specifier - Required. A character which indicates how an argument's data should be represented. The list of possible characters is shown in the table below. ... char var1 = 102; short int var2 = 2024; int var3 = 95; long int var4 = 212; long long int var5 = 1200L; printf("%hhd %hd %d %ld %lld", var1, var2, var3, var4, var5); Try it Yourself »
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › numberformat.html
Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
There are many converters, flags, and specifiers, which are documented in java.util.Formatter ... The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is: ... The printf and format methods are overloaded.
🌐
W3Schools
w3schools.com › java › java_output.asp
Java Output Values / Print Text
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
GeeksforGeeks
geeksforgeeks.org › java › formatted-output-in-java
Formatted Output in Java using printf() - GeeksforGeeks
August 16, 2024 - // Java Programs to demonstrate ... Double Value with // different Formatting System.out.printf("%f\n", a); System.out.printf("%5.3f\n", a); System.out.printf("%5.2f\n", a); } }...
🌐
DEV Community
dev.to › realedwintorres › how-to-formatting-java-output-with-printf-1bik
How-To: Formatting Java Output With printf() - DEV Community
November 7, 2021 - You can format other Java data types too. To format an integer type, use %d. To format a floating-point value, use %f. With floating-point formats, you can specify the field width and the number of decimal places.
🌐
Java67
java67.com › 2014 › 06 › how-to-format-float-or-double-number-java-example.html
5 Examples of Formatting Float or Double Numbers to String in Java | Java67
Alternatively, if you just want to print formatted floating point numbers into console, you can use System.printf() method, which effectively combine above two lines into single one. But the best method for task is using DecmialFormat class, which ...
🌐
CodeGym
codegym.cc › java blog › strings in java › printf() in java
Printf() in Java
December 23, 2024 - printf() method belongs to the Java standard library and plays an important role in formatting output. This article will provide beginners with a comprehensive understanding of the printf() method, its purpose, and how to use it effectively...