HOWTO · Java

How to Print Variables in Java

Learn how to print Java variables with print(), println(), and printf(), including their line-break and formatting behavior.

On this page

To print Java variable values, pass them to System.out.print(), System.out.println(), or System.out.printf(). Use print() to continue on the current line, println() to finish a line, and printf() when you need formatted values. All three write to standard output; the choice depends on how you want the text arranged.

System.out is the standard output stream provided by the System class. Its print() method writes the supplied value and does not append a line break. You can pass a variable directly or combine labels and values with the + operator. That makes print() useful when several pieces should appear on one line.

Run this example:

public class VariablePrintingExample {
  public static void main(String[] args) {
    String productName = "Laptop";
    double price = 899.99;
    int quantity = 3;

    System.out.print("Product: " + productName + ", Price: $" + price + ", Quantity: " + quantity);
    System.out.println();
    System.out.println("Available: " + quantity);
    System.out.printf("Price: $%.2f%n", price);
  }
}
Product: Laptop, Price: $899.99, Quantity: 3
Available: 3
Price: $899.99

The supplied run completed successfully and produced this output. The first call writes the product details without ending the line; the next call adds a line break. The remaining calls demonstrate a labeled value on its own line and a price formatted to two digits after the decimal point.

print() requires an argument. A bare call such as System.out.print() does not compile because PrintStream has no no-argument print() overload. Supply a value or use the no-argument println() when you only need a line break.

System.out.println(value) writes the value and then advances to a new line. Use it for separate labels or records so each appears on its own line. It accepts the same kinds of values as print(), including strings, numbers, and variables.

Unlike print(), println() also has a no-argument form: System.out.println(). It writes only a line break, which is useful after several print() calls have assembled one line. A newline is added after each call, so consecutive calls do not continue on the same line.

For example, calls that print "Product: " + productName, "Price: $" + price, and "Quantity: " + quantity each produce a separate line when passed to println(). This keeps the value and its label together without needing to insert a newline character into the string.

Use System.out.printf() when the output needs a specific layout, such as a fixed number of decimal places. Its first argument is a format string; conversion specifiers in that string describe the corresponding values that follow. Common conversions include %s for a string, %d for an integer, and %f for a floating-point number. For example, %.2f formats a floating-point value with two digits after the decimal point.

Each conversion must have a compatible supplied argument. Too few arguments or an incompatible argument cause a formatting error at runtime, while unused extra arguments with no corresponding conversion are ignored. A call such as System.out.printf("%d", "three") is not a compile-time type error: Java accepts the argument list, but formatting fails at runtime because %d cannot format a string. Use %s for text or pass an integer to %d. A missing value for a conversion also causes a formatting error when the call runs.

printf() does not automatically end the line. Include %n in the format string when you want a platform-appropriate line separator. For example, System.out.printf("Price: $%.2f%n", price) formats the value and then ends the line. Without %n, another write can continue on the same line.

Choose between print(), println(), and printf()

Choose print() when you want to write a value and continue on the same line. Choose println() for line-oriented output or when you need to add a line break by itself. Choose printf() when a format string gives you useful control over spacing, decimal places, or other conversions. You can combine these methods in one program; each call writes to the same standard output stream.

For simple labels and values, concatenation with print() or println() is usually easy to read. For repeated numeric formatting, printf() keeps the layout in one format string. Check that every conversion matches its value, and add %n explicitly if the formatted output should end its line. None of these methods is inherently more efficient for every task; select the one whose line behavior and formatting make the result clearest.