Codectionary / Developer documentation / Java

System.out.println / print / printf

System.out is Java's standard output stream, and it exposes three main methods for writing text to the console. println() prints its argument followed by a newline, print() prints without adding a newline, and printf() gives C-style formatted output using format specifiers like %d, %s, and %.2f for precise control over how values are displayed.

Syntax

System.out.println(value);

Examples

println vs print

The core difference between the two output methods.

System.out.println("Hello, World!");
System.out.println(42);

System.out.print("No new line here... ");
System.out.print("still the same line.");
System.out.println();  // move to a new line manually

Concatenating Output

Building output strings by combining text and variables with the + operator.

String name = "Fola";
int score = 87;

System.out.println("Name: " + name + ", Score: " + score + "%");

Formatted Output with printf

Using format specifiers for precise control over number formatting and alignment.

double price = 19.5;
int quantity = 3;
String item = "Keyboard";

System.out.printf("Item: %s, Qty: %d, Price: $%.2f%n", item, quantity, price);
// Item: Keyboard, Qty: 3, Price: $19.50

System.out.printf("%-10s|%5d%n", "Score", 95);  // left/right alignment

Best practices

  • Use println() for most output - reach for printf() specifically when you need controlled number formatting or column alignment
  • Use %n rather than \\n inside printf() format strings for a platform-independent newline
  • Avoid excessive string concatenation with + in loops - use a StringBuilder instead for building large strings efficiently
  • Remember System.out is meant for simple console output - use a proper logging framework (like SLF4J) in real applications

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

Variables & Primitive Data Types
Java is a statically-typed language, meaning every variable must be declared with an explicit type before use, and that type cannot change afterward. Java has eight primitive types - byte, short, int, long, float, double, char, and boolean - which store simple values directly rather than references, making them fast and memory-efficient. Anything beyond primitives (String, arrays, custom classes) is a reference type instead.
Comments & Javadoc
Java supports single-line comments with //, multi-line comments with /* */, and a special documentation format called Javadoc, written as /** */. Javadoc comments support tags like @param, @return, and @throws, and can be processed by the javadoc tool to automatically generate HTML API documentation - the same format used for Java's own official standard library docs.
Arithmetic & Assignment Operators
Java provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder (modulus). It also has compound assignment operators (+=, -=, etc.) that combine an operation with assignment, along with increment (++) and decrement (--) operators in both prefix and postfix forms, which subtly differ in when the value is updated relative to being used.
Comparison & Logical Operators
Comparison operators (==, !=, <, >, <=, >=) compare two values and evaluate to a boolean. Logical operators (&&, ||, !) combine or invert boolean expressions, with && and || short-circuiting - meaning the second operand is skipped entirely if the first already determines the result. Importantly, == compares object references for non-primitive types like String, so .equals() should be used to compare their actual content.