Advanced Java and JVM· 35 min

I/O, NIO, Reflection, and Modules

Work with Path and Files, channels, buffers, reflection, annotations, serialization, and the module system.

Java interview lesson
Question 11 · Not reviewed

Advanced Java · platform APIs

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

I/O, NIO, Reflection, and Modules

Explain it aloud in your own words

Strong answer

NIO provides Path, Files, channels, buffers, and selectors for modern file and network I/O. Reflection inspects or invokes members dynamically but weakens compile-time guarantees. Modules make dependencies and exported packages explicit, improving boundaries and startup analysis.

Key points

  • Use try-with-resources for closeable resources.
  • Buffer state has position, limit, and capacity; confusing them causes subtle bugs.
  • Reflection is powerful but should be isolated behind a stable abstraction.
  • A module's exports and opens have different meanings.

Interviewer looks for

Compare the convenience and safety of ordinary APIs with the flexibility and cost of reflection or dynamic access.

Java example

1try (var lines = Files.lines(Path.of("users.txt"))) {
2  long count = lines.filter(line -> !line.isBlank()).count();
3  System.out.println(count);
4}

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Basic

Why should Java I/O use try-with-resources?

Strong answer

It closes AutoCloseable resources deterministically even when an exception occurs. It also preserves suppressed exceptions, which is safer than manually closing resources in a fragile finally block.

Q2Intermediate

What are position, limit, and capacity in a NIO Buffer?

Strong answer

Capacity is the fixed storage size, position is the next index read or written, and limit is the boundary for the current operation. flip changes a buffer from writing mode to reading mode by setting limit to the current position and position to zero.

Q3Intermediate

What are the risks of reflection?

Strong answer

Reflection weakens compile-time checks, can fail at runtime, complicates refactoring, and may interact with module access rules. Isolate it behind a small abstraction and validate failures clearly.

Q4Intermediate

What is the difference between exports and opens in Java modules?

Strong answer

exports allows ordinary access to public types from another module. opens allows deep reflective access at runtime, usually for frameworks, without making the package part of the normal compile-time API.

Q5Intermediate

When would you choose Files.lines over reading all lines?

Strong answer

Files.lines gives a lazy stream and can reduce peak memory for large files, but the stream still owns an open resource and must be closed. For small files, readAllLines can be simpler and clearer.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.