Avoid Comparisons Against Character.MAX_VALUE / Character.MIN_VALUE

ID

java.char_compare_against_max_min

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

reliability, suspicious-comparison

Description

Reports relational comparisons (<, >, <=, >=) where one operand is Character.MAX_VALUE or Character.MIN_VALUE. Such comparisons are always true or always false for any char value, which indicates a logic error.

Rationale

Character.MAX_VALUE is '\uFFFF' (65535) and Character.MIN_VALUE is '\u0000' (0). Since a char value is always in the range [0, 65535], comparisons like c ⇐ Character.MAX_VALUE or c >= Character.MIN_VALUE are tautologically true, making the conditional branch misleading or dead. This usually reveals a misunderstanding of the constant’s purpose or a copy-paste error.

// Bad -- comparison is always true for any char
if (c < Character.MAX_VALUE) {
    process(c);
}

// Bad -- comparison is always true
if (c >= Character.MIN_VALUE) {
    process(c);
}

Remediation

Replace the boundary constant with the actual domain limit the code intends to enforce:

// Good -- use a meaningful domain bound
if (c < 'z') {
    process(c);
}

// Good -- explicit range check
if (c >= 'A' && c <= 'Z') {
    process(c);
}