Boundary checks test the values just inside and outside a valid range.

range guard The guard checks lower and upper bounds before indexing the array.
edge case Values such as `-1`, `0`, and the array length reveal off-by-one mistakes.

Boundary Test

index
BoundaryTest.java
Replay: real traced execution (multi-file project)
public class BoundaryTest {
    static int readOrDefault(int[] values, int index) {
        if (index >= 0 && index < values.length) {
            return values[index];
        }
        return -1;
    }

    public static void main(String[] args) {
        int index = 1;
        int[] values = {10, 20, 30};
        int selected = readOrDefault(values, index);

        System.out.println("selected=" + selected);
    }
}
public class BoundaryTest {
    static int readOrDefault(int[] values, int index) {
        if (index >= 0 && index < values.length) {
            return values[index];
        }
        return -1;
    }

    public static void main(String[] args) {
        int index = -1;
        int[] values = {10, 20, 30};
        int selected = readOrDefault(values, index);

        System.out.println("selected=" + selected);
    }
}
public class BoundaryTest {
    static int readOrDefault(int[] values, int index) {
        if (index >= 0 && index < values.length) {
            return values[index];
        }
        return -1;
    }

    public static void main(String[] args) {
        int index = 3;
        int[] values = {10, 20, 30};
        int selected = readOrDefault(values, index);

        System.out.println("selected=" + selected);
    }
}
  1. index ← 1

    9public static void main(String[] args) {10    int index→ 1 = 1;  //@index=-1, 311    int[] values = {10, 20, 30};12    int selected = readOrDefault(values, index1);
  2. static int readOrDefault(int[] values, int index)

    1public class BoundaryTest {2    static int readOrDefault(int[] values, int index1) {3        if (index >= 0 && index < values.length) {
  3. if (index >= 0 && index < values.length)

    2static int readOrDefault(int[] values, int index) {3    if (index1 >= 0 && index < values.length3) {4        return values[index]20;5    }
  4. selected ← 20

    11    int[] values = {10, 20, 30};12    int selected→ 20 = readOrDefault(values, index1);1314    System.out.println("selected=" + selected20);15}
    outputselected=20
  1. index ← -1

    9public static void main(String[] args) {10    int index→ -1 = -1;11    int[] values = {10, 20, 30};12    int selected = readOrDefault(values, index-1);
  2. static int readOrDefault(int[] values, int index)

    1public class BoundaryTest {2    static int readOrDefault(int[] values, int index-1) {3        if (index >= 0 && index < values.length) {4            return values[index];5        }6        return -1;7    }
  3. selected ← -1

    11    int[] values = {10, 20, 30};12    int selected→ -1 = readOrDefault(values, index-1);1314    System.out.println("selected=" + selected-1);15}
    outputselected=-1
  1. index ← 3

    9public static void main(String[] args) {10    int index→ 3 = 3;11    int[] values = {10, 20, 30};12    int selected = readOrDefault(values, index3);
  2. static int readOrDefault(int[] values, int index)

    1public class BoundaryTest {2    static int readOrDefault(int[] values, int index3) {3        if (index >= 0 && index < values.length) {4            return values[index];5        }6        return -1;7    }
  3. selected ← -1

    11    int[] values = {10, 20, 30};12    int selected→ -1 = readOrDefault(values, index3);1314    System.out.println("selected=" + selected-1);15}
    outputselected=-1