Take the 2-minute tour ×

There is a PMD rule saying one should avoid to instantiate BigInteger or BigDecimal if there is a predefined constant.


// instead of

new BigInteger(0)

Would there be any other advantage than saving a few bytes?

share|improve this question
Its about 48 bytes. But it depends on how much its is called as to whether it matters. If performance is critical then BigInteger may not be the best solution. –  Peter Lawrey Oct 6 '11 at 13:09

5 Answers 5

up vote 4 down vote accepted

it avoids the allocation of those few bytes and the need to collect them back later

in a tight loop that can matter

share|improve this answer

Yes, saving a few JVM instructions.

share|improve this answer

Possibly performance, if you are instantiating a lot of 0s. An alternative for long/int argument is


which returns BigInteger.ZERO when argument is 0

share|improve this answer

By using cached values, it is likely to yield significantly better space and time performance.

share|improve this answer

Instead of creating a new object with new BigInteger you'd better use one static object which is created once when the BigInteger class is loaded. It is also true for using valueOf of all wrapper types.

share|improve this answer
Do you mean that valueOf uses those constants? –  deamon Oct 6 '11 at 12:55
@deamon yep check docjar.com/docs/api/java/math/BigInteger.html#valueOf%28long%29 at the bottom –  ratchet freak Oct 6 '11 at 13:33
@deamon Yes, valueOf uses a pool of the most frequently used values. For example, BigInteger.valueOf caches 33 values from -16 to 16, so when you invoke it for these values you'll always get the same BitInteger object for one value. BigInteger.ZERO is just BigInteger.valueOf(0). –  tcb Oct 6 '11 at 15:40

Your Answer


