Magic number
Reports "magic numbers": numeric literals that are not named by a constant declaration.
Using magic numbers can lead to unclear code, as well as errors if a magic number is changed in one location but remains unchanged not another. The numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1000, 0L, 1L, 2L, 0.0, 1.0, 0.0F and 1.0F are not reported by this inspection.
Example:
A quick-fix introduces a new constant:
Configure the inspection:
Use the Ignore constants in 'hashCode()' methods option to disable this inspection within
hashCode()
methods.Use the Ignore in annotations option to ignore magic numbers in annotations.
Use the Ignore initial capacity for StringBuilders and Collections option to ignore magic numbers used as initial capacity when constructing
Collection
,Map
,StringBuilder
orStringBuffer
objects.
Inspection options
Option | Type | Default |
---|---|---|
Ignore constants in 'hashCode()' methods | Checkbox | true |
Ignore in annotations | Checkbox | true |
Ignore initial capacity for StringBuilders and Collections | Checkbox | false |
Inspection Details | |
---|---|
Available in: | IntelliJ IDEA 2023.3, Qodana for JVM 2023.3 |
Plugin: | Java, 233.SNAPSHOT |