Input and Output in Java

🧠 Topic: Input and Output in Java


💻 What is Input and Output?

Input → This is how a program receives data.
Output → This is how a program shows or returns data.

Think of it like this:

  • Input = You talking to the computer (e.g., typing or clicking).

  • Output = Computer talking back to you (e.g., showing results on screen).

Example:

  • When you type something in a form → that’s input.

  • When the program displays your result → that’s output.


🖨 OUTPUT in Java

Output is basically the program’s response — the result shown after processing something.

In Java, we use:

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

Let’s break that down 👇

Part

Meaning

System

A built-in Java class that handles system-level stuff (like input/output).

out

Refers to the standard output device (usually your screen).

println()

A method that prints text and then moves the cursor to the next line.

So, this line:

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

➡ Displays Hello World! and moves the cursor to the next line.

If you remove the “ln”, like this:

System.out.print("Hello World!");

➡ It’ll still print Hello World!, but the cursor stays on the same line.


Examples of Output Methods

Code

Output

Explanation

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

Hello
World!

Each println moves to a new line

System.out.print("Hello");
System.out.print("World!");

HelloWorld!

Stays on one line (no space)

System.out.print("Hello ");
System.out.print("World!");
System.out.println();

Hello World!

Adds space + moves to next line

Pro Tip 💡:
System.out.println(); by itself makes a blank line — but only if there’s no print before it.


INPUT in Java

Now let’s talk about getting input from the user (like typing something in the console).

Java doesn’t support input directly — we need a Scanner to do that.

You start with:

import java.util.Scanner;

This line imports the Scanner class, which allows the program to read input from the keyboard.


🧩 Sample Input Program
import java.util.*;  

class InputOutput {
    public static void main(String[] args) {
        int num;
        Scanner scanner;
        scanner = new Scanner(System.in);
        
        System.out.print("Enter an integer: "); // prompt
        num = scanner.nextInt();
        
        System.out.println("The integer is " + num);
    }
}

🕵‍♀️ Step-by-Step Explanation
  1. import java.util.*;

    • This gives access to the Scanner class (and other useful ones).

    • The * means “import everything” in that package.

  2. Scanner scanner = new Scanner(System.in);

    • Creates a Scanner object named scanner.

    • System.in = standard input device (keyboard).

  3. System.out.print("Enter an integer: ");

    • Displays a prompt (so the user knows what to type).

  4. num = scanner.nextInt();

    • Reads the integer typed by the user and stores it in the variable num.

  5. System.out.println("The integer is " + num);

    • Shows the output (the number entered).

Output Example:

Enter an integer: 8
The integer is 8

🔍 Other Useful Scanner Methods

Method

What it Reads

Example

nextInt()

Integer (whole number)

8

nextDouble()

Decimal number

3.14

next()

Single word

"Hello"

nextLine()

Whole line of text

"Hello World"


Quick Comparison: Input vs Output

Feature

Input

Output

Purpose

To get data from user

To show data to user

Device Used

Keyboard, mouse, etc.

Monitor, printer, etc.

Java Keyword

System.in

System.out

Common Method

nextInt(), nextLine()

print(), println()


🧩 Summary / In Short:
  • Input = what the computer receives.

  • Output = what the computer shows back.

  • Scanner → for input.

  • System.out.print / println → for output.

  • ln means “move to the next line.”


💡 Memory Trick (Para di mo makalimutan!)

🧠 Think of I/O as “In & Out” — like breathing!

  • You inhale = Input (get data)

  • You exhale = Output (show data)

Or use this mnemonic:

“I Scan In, I Print Out.”
(Scanner for Input, Print for Output 😉)