DEV Community

Hanna
Hanna

Posted on

πŸ“ 06. Java Self-Study: Variables & System I/O

  • Date: 2026-03-12
  • Topic: Chapter02_sec04 (Variables and System I/O)

1. System I/O (System.in / System.out)

I finally understand the meaning behind System.out.println(), which I had been using without much thought.

  • System.out (Standard Output): Monitor
  • System.in (Standard Input): Keyboard

2. Output Methods: print, println, and printf

There are more ways to output data than just println.

  • print(): Outputs content without a newline.
  • println(): Outputs content and moves to the next line.
  • printf(): Outputs formatted content. This is essential for controlling decimal places or aligning data in professional projects.

πŸ’‘ Understanding the Format Specifier: %-10.2f

It looked complicated at first, but it's actually quite logical:

  • %: Start of the format specifier.
  • -: Left-aligned (remaining space on the right is filled with blanks).
  • 10: Total width (occupies 10 spaces, including the decimal point).
  • .2: Limits the output to 2 decimal places.
  • f: Formats the value as a floating-point number.

Note: You can specify the order using %1$d, %2$s, but Java is smart enough to match them in order automatically. Manual ordering is only necessary when reusing the same variable or changing the sequence.


3. Keyboards and KeyCodes

Computers process numbers faster than characters, so every key on a keyboard is assigned a unique number called a KeyCode.

  • System.in.read(): Reads a single KeyCode.
  • Important: When using this, you must include throws Exception. It’s like a safety valve where Java "passes the buck" if something goes wrong with the input.
  • Pro Tip: Experienced developers often use try-catch blocks instead of throws to handle errors gracefully and provide helpful feedback to the user.

4. Scanner: A Smarter Way to Handle Input

Since KeyCodes only read one byte at a time, they struggle with multi-byte characters like Korean. That's where the Scanner class comes in.

  • Setup: import java.util.Scanner; (Like taking a tool out of the Java storage room).
  • Shortcut: In Eclipse, pressing Ctrl + Shift + O automatically manages your imports.

πŸ—οΈ Reference Types and the new Operator

Unlike basic types like int, Scanner is a Reference Type (Class).

  • Class: A "blueprint" for creating an object.
  • new: A command to actually build the object in memory based on that blueprint.
  • Scanner(System.in): "Build a scanner that is connected to the keyboard."

Breaking down the code: Scanner scanner = new Scanner(System.in);
-> "Create a physical scanner object that reads from the keyboard and store it in the box named 'scanner'!"

  • scanner.nextLine(): Very convenient because it reads an entire line until you hit Enter.

🏁 Wrapping Up

Learning how data is formatted and how keyboard input is stored in variables was fascinating. It feels like I'm finally learning features that are used in the real world. That's the end of Section 2 (Variables)! Starting Section 3 (Operators) tomorrow. πŸ”₯

Top comments (0)