Let’s break it down simply 👇
🧩 1. System.in
-
Systemis a built-in Java class that gives access to system-level input and output. -
inis a static field inside theSystemclass. -
System.inrepresents standard input stream — usually the keyboard.
💡 In short:
System.in = the stream that takes input from the keyboard.
🧮 2. nextInt() (from Scanner class)
-
nextInt()is a method of theScannerclass. -
It reads the next integer value from the input stream connected to the scanner.
Example:
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // connect scanner to keyboard input
System.out.print("Enter a number: ");
int num = sc.nextInt(); // reads an integer from user
System.out.println("You entered: " + num);
sc.close();
}
}
🔍 Step-by-step of what happens:
-
new Scanner(System.in)→ connects the scanner to keyboard input. -
sc.nextInt()→ waits for the user to type an integer and press Enter. -
It reads that integer and stores it in a variable (like
num).
🧠 Summary Table
| Expression | Type | Meaning |
|---|---|---|
System.in |
Input Stream | Takes input from keyboard |
new Scanner(System.in) |
Scanner Object | Reads input from user |
nextInt() |
Method | Reads the next integer value entered |
Would you like me to also explain what happens internally (how Java converts the typed number to an integer)?
No comments:
Post a Comment