liveBook · Manning

Boundary check

As explained in 100 Java Mistakes and How to Avoid Them

A bounds check is a validation that an index or indexed range lies within the permitted limits of a data structure.

Bounds checking validates that an index or indexed range lies within a data structure’s permitted limits. Java prevents out-of-range access with exceptions, but explicit checks still prevent empty-input failures, incorrect behavior, data corruption, and security holes. section 5.2

Avoid these common checking mistakes:

  • Accessing an element before checking its index, such as calling charAt(0) on a possibly empty string. section 5.2

Prefer standard range checks

Custom array- and collection-like APIs can use Objects.checkIndex, Objects.checkFromToIndex, and Objects.checkFromIndexSize before accessing elements or ranges.

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains empty inputs, ordering errors, reversed comparisons, off-by-one errors, range constraints, and standard library checks.
Examples
Uses empty strings, array element access, offset-count ranges, and custom InputStream and List implementations.
What it assumes
Assumes familiarity with Java indexed access and teaches the boundary cases programmers commonly overlook.
What you can do after
Diagnose/debug with it by locating unsafe access order, reversed comparisons, and off-by-one conditions.
How it connects
Connects arrays, strings, lists, exceptions, input validation, standard library APIs, and static analysis.

Also explained in

Compare how 2 books explain boundary check →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.