Java binary search and interval bounds

An inclusive interval tracks where the target could still be. Each comparison either returns a match or removes the midpoint and one half of the candidates.

Java
class Main {
    static int search(int[] values, int target) {
        int low = 0, high = values.length - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (values[mid] == target) return mid;
            if (values[mid] < target) low = mid + 1;
            else high = mid - 1;
        }
        return -1;
    }
    public static void main(String[] args) {
        System.out.println(search(new int[]{2, 4, 6, 8, 10}, 8));
        System.out.println(search(new int[]{}, 8));
        if (search(new int[]{2, 4}, 3) != -1) throw new AssertionError("missing");
    }
}
Read the full lesson