Why avoid creating a biginteger instance in Java

There is a PMD rule in which you should avoid creating an instance of BigInteger or BigDecimal if there is a predefined constant.

BigInteger.ZERO // instead of new BigInteger(0) 

Would there be another advantage besides storing a few bytes?

+4
source share
5 answers

he avoids allocating these few bytes and having to collect them later.

in a tight loop that can make a difference

+5
source

Yes, keeping a few JVM instructions.

+2
source

Perhaps performance if you create a lot of 0s. An alternative for the long / int argument is

BigInteger.valueOf(0)

which returns BigInteger.ZERO when the argument is 0

+2
source

Instead of creating a new object with new BigInteger you better use a single static object that is created once when the BigInteger class is loaded. This is also true for using valueOf all types of wrappers.

+1
source

Using cached values, it is likely to yield significantly better performance in space and time.

+1
source

All Articles