Basic Syntax
Input and Output
Deconstruct console writing via System.out/err and input handling using Scanner and BufferedReader.
Interview: Tests Scanner buffer leaks, BufferedReader efficiency for competitive programming, and I/O streams handling.
Input and Output (I/O) operations allow a program to interact with external environments. Java provides System.out and System.err for console writing, and the Scanner and BufferedReader classes for reading user input.
Core Idea
Scanner parses formatted data tokens (using regex). BufferedReader reads raw character streams efficiently using buffering.
Why It Matters
Using Scanner in competitive programming can cause timeouts due to parsing overhead, while failing to close inputs leaks file handles.
Interview Lens
Focuses on token buffering traps (like nextInt and nextLine issues) and resource closing using try-with-resources.
Console Output Streams
System.out(Standard Output): A buffered output stream used for standard application prints.System.err(Standard Error): An unbuffered output stream used for logging error conditions immediately.- Formatting Methods:
print(): Prints text without a newline.println(): Prints text and appends a newline.printf()/format(): Writes formatted strings using converters (e.g.%dfor integers,%sfor strings).
Console Input: Scanner vs BufferedReader
1. Scanner Class
A high-level utility in java.util that parses primitives and strings using regular expressions.
- Pros: Easy to use; parses input directly into types (e.g.
nextInt()). - Cons: Slower due to regex matching; parsing large files can cause performance issues.
2. BufferedReader Class
A buffered character-input stream reader in java.io.
- Pros: Extremely fast; reads large chunks of data into memory efficiently.
- Cons: Only reads raw strings (requires manual parsing using
Integer.parseInt); throws checked exceptions (IOException).
Common Pitfalls
- The nextLine() Buffer Trap: When using a
Scanner, callingnextInt()ornext()does not consume the trailing newline character (). A subsequent call tonextLine()will immediately consume this newline and return an empty string, skipping user input. To fix this, insert an extra dummynextLine()call to clear the buffer. - Failing to Close Streams: Forgetting to close input streams (like
ScannerorBufferedReader) leaks operating system-level file descriptors and resources.
Best Practices
- Use Try-With-Resources: Always wrap I/O resources in a try-with-resources statement to ensure they are closed automatically, even if exceptions occur.
- Use BufferedReader for Competitive Programming: When processing large inputs in programming contests, always prefer
BufferedReaderoverScannerto avoid performance bottlenecks.
Interview-Relevant Information
Q1: How do you resolve the Scanner nextLine() buffer trap?
Answer: Add an extra scanner.nextLine() call directly after scanner.nextInt() to consume the remaining newline character before reading the next line of input.
Q2: Why is BufferedReader faster than Scanner?
Answer: BufferedReader reads character streams using a large internal memory buffer (default 8KB), performing fewer read operations on the underlying stream. Scanner parses bytes using regular expressions, which adds CPU overhead.
Quick Checklist
Can you explain the difference between System.out and System.err, describe the nextLine() buffer trap, and explain why BufferedReader is more efficient than Scanner? If yes, you understand Java I/O.
Use Cases
Parsing console configuration parameters using Scanner in local CLI tools.
Building fast competitive programming algorithms that parse input streams using BufferedReader.
Common Mistakes
Leaving out the buffer clearance line between nextInt() and nextLine() calls when using a Scanner.
Forgetting to wrap BufferedReaders in Try-With-Resources, causing system socket or file descriptor leaks.