Java — Output
System.out.println
The most basic output command in Java:
System.out.println("Hello, world!");
println prints the text and moves to the next line. Think of it as "print line."
System.out.print vs System.out.println
System.out.print("Hello ");
System.out.print("world!");
System.out.println();
System.out.println("Done.");
Output:
Hello world!
Done.
print stays on the same line. println adds a newline after printing. Use print when you want to build output piece by piece.
Printing numbers and expressions
System.out.println(42);
System.out.println(3.14);
System.out.println(2 + 3);
System.out.println("Result: " + (2 + 3));
Output:
42
3.14
5
Result: 5
Notice the difference: 2 + 3 without quotes does math. "Result: " + (2 + 3) concatenates a string with the result. Parentheses force the math to happen first.
Concatenation
The + operator joins strings together:
String greeting = "Hello";
String name = "Ada";
System.out.println(greeting + ", " + name + "!");
Output:
Hello, Ada!
Every value can be converted to a string automatically when joined with one. Java calls this string conversion — numbers, booleans, even objects become their text representation.
printf — formatted output
For precise control over formatting, use printf:
String name = "Ada";
int age = 36;
double gpa = 3.85;
System.out.printf("Name: %s%n", name);
System.out.printf("Age: %d%n", age);
System.out.printf("GPA: %.2f%n", gpa);
Output:
Name: Ada
Age: 36
GPA: 3.85
Common format specifiers:
| Specifier | Meaning | Example |
|---|---|---|
%s | String | "Ada" |
%d | Integer | 42 |
%f | Float/double | 3.85 |
%.2f | Float with 2 decimals | 3.85 |
%n | Platform-independent newline | \n on most systems |
printf shines when you need aligned columns or precise decimal places — things println can't do cleanly.
Reading user input with Scanner
Java doesn't have a built-in input() function like Python. Instead, you use the Scanner class:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("What is your name? ");
String name = sc.nextLine();
System.out.print("How old are you? ");
int age = sc.nextInt();
System.out.printf("Hello %s, you are %d years old.%n", name, age);
sc.close();
}
}
Output:
What is your name? Ada
How old are you? 36
Hello Ada, you are 36 years old.
Three things to note:
nextLine()reads an entire line of textnextInt()reads a single integer — if the user types text, it crashes- Always
close()the scanner when done to free system resources
Scanner methods overview
| Method | Reads |
|---|---|
nextLine() | Entire line of text |
next() | Single word (stops at whitespace) |
nextInt() | An integer |
nextDouble() | A decimal number |
nextBoolean() | A boolean value |
The method you choose must match what the user types. Typing "hello" when nextInt() expects a number throws an InputMismatchException. Validate or catch it.
println with multiple values
int x = 5;
int y = 10;
System.out.println("x = " + x + ", y = " + y);
Output:
x = 5, y = 10
Java concatenates everything into one string before printing. There's no limit to how many values you chain together.
Escaping special characters
Some characters can't be typed directly inside a string:
System.out.println("She said \"hello\"");
System.out.println("Line one\nLine two");
System.out.println("Path: C:\\Users\\Ada");
Output:
She said "hello"
Line one
Line two
Path: C:\Users\Ada
| Escape | Represents |
|---|---|
\" | Double quote |
\n | Newline |
\t | Tab |
\\ | Backslash |
Mini Practice
- Print a receipt: item name, quantity, and price on separate lines
- Use
printfto print a number with exactly 3 decimal places - Ask the user for their name and birth year — calculate and print their age
- Print a multi-line poem using
\nescape sequences - Try using
nextInt()thennextLine()— observe the newline bug and fix it with an extranextLine()call
Next: commenting your code effectively →
Related Topics
Frequently Asked Questions about Output
What is Output in Java?
Output is a fundamental concept in Java. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Output?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Output.
Why is Output important in Java?
Output is essential for Java development. Understanding this concept will help you write better code and solve real-world problems more effectively.