Notes / Java / Input & Output

Input & Output in Java — Revision Notes

A quick-reference cheat sheet: definition, syntax, example, common mistakes and an interview tip.

beginner 📖 2 min revision

Definition

📌 Input & Output Java prints output with System.out.println() (or print() without a newline).

Syntax

syntax
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
String s = sc.nextLine();

Example

java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your age: ");
        int age = sc.nextInt();
        System.out.println("You are " + age + " years old.");
    }
}

Output

outputEnter your age: 20 You are 20 years old.

Important Points

Common Mistakes

Interview Tip

🎯 Before your interview The nextInt()/nextLine() mixing bug is a classic "gotcha" question in Java interviews — know how to explain and fix it.
Back to notes Read full lesson →