Back to Intellij Community

ConstantValue

java/java-impl/resources/inspectionDescriptions/ConstantValue.html

2025.3-rc-21.5 KB
Original Source

Reports expressions and conditions that always produce the same result, like true, false, null, or 0. Such expressions can be replaced with the corresponding constant value, or may indicate there is a bug in the code.

Examples:

// always true
  // cause: || is used instead of &&
  if (x > 0 || x < 10) {}

  System.out.println(str.trim());
  // always false
  // cause: variable was dereferenced before null-check
  if (str == null) {}

The inspection's behavior may be controlled by annotations, such as nullability annotations, @Contract annotation, @Range annotation and so on.

Configure the inspection:

  • Use the Don't report assertions with condition statically proven to be always true option to avoid reporting assertions that were statically proven to be always true. This includes conditions like if (alwaysFalseCondition) throw new IllegalArgumentException();.
  • Use the Ignore assert statements option to control how the inspection treats assert statements. By default, assertions are assumed to be executed (-ea mode). When the option is enabled, assertions will be ignored (-da mode).
  • Use the Warn when constant is stored in variable option to display warnings when a variable is used, whose value is known to be constant.

Before IntelliJ IDEA 2022.3, this inspection was part of the Constant Conditions & Exceptions inspection. This inspection has been split into two inspections: Constant Values and Nullability and data flow problems.