File: ConstantOverflow.md

package info (click to toggle)
error-prone-java 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,076 kB
  • sloc: java: 171,398; xml: 1,459; sh: 34; makefile: 7
file content (23 lines) | stat: -rw-r--r-- 936 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Compile-time constant expressions that overflow are a potential source of bugs.

Literals without an explicit `L` suffix have type `int`, so the following
multiplication expression is evaluated as an integer before being widened to
`long`. The value is greater than `Integer.MAX_VALUE`, so it wraps around to
`-1857093632`.

```java
static final long NANOS_PER_DAY = 24  * 60 * 60 * 1000 * 1000 * 1000;
```

The intent was probably for the multiplication expression to be evaluated as a
`long` instead of an `int`.

```java
static final long NANOS_PER_DAY = 24L * 60 * 60 * 1000 * 1000 * 1000;
```

If you find yourself doing this kind of time based math, consider using an API
that provides a safer, more readable solution. For example, on the sub-second
scale, see [`TimeUnit.convert`].

[`TimeUnit.convert`]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/TimeUnit.html#convert(long,%20java.util.concurrent.TimeUnit)