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 |
|---|---|
| A built-in Java class that handles system-level stuff (like input/output). |
| Refers to the standard output device (usually your screen). |
| 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 |
|---|---|---|
| Hello | Each |
| HelloWorld! | Stays on one line (no space) |
| 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
import java.util.*;This gives access to the Scanner class (and other useful ones).
The
*means “import everything” in that package.
Scanner scanner = new Scanner(System.in);Creates a Scanner object named
scanner.System.in= standard input device (keyboard).
System.out.print("Enter an integer: ");Displays a prompt (so the user knows what to type).
num = scanner.nextInt();Reads the integer typed by the user and stores it in the variable
num.
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 |
|---|---|---|
| Integer (whole number) | 8 |
| Decimal number | 3.14 |
| Single word | "Hello" |
| 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 |
|
|
Common Method |
|
|
🧩 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 😉)