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 manuallyConcatenating Output
Building output strings by combining text and variables with the + operator.
String name = "John";
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 alignmentBest 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
Runtime & versions
Java Virtual Machine. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Java Language Specification