Jump to content

Unreachable code

From Simple English Wikipedia, the free encyclopedia
Revision as of 01:01, 4 February 2022 by NguoiDungKhongDinhDanh (talk | changes) (Fix old source tag)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Unreachable code is a section of code in a program that can never be executed. [1]

Having unreachable code is bad because it wastes space on the disk, memory, and CPU cache.

Luckily, many modern compilers alert the programmer with a warning or error whenever it detects unreachable code.

  void beepForever() {
      while (true) {
          System.out.println("Beep!");
      }
      System.out.println(":'(");
  }

In this example, ":'(" will never be printed because the loop never exits.


References

[change | change source]
  1. "Java 7 Language Specification".