Th开发者_Python百科ere is a PMD rule saying one should avoid to instantiate BigInteger or BigDecimal if there is a predefined constant.
BigInteger.ZERO
// instead of
new BigInteger(0)
Would there be any other advantage than saving a few bytes?
it avoids the allocation of those few bytes and the need to collect them back later
in a tight loop that can matter
Yes, saving a few JVM instructions.
Possibly performance, if you are instantiating a lot of 0s. An alternative for long/int argument is
BigInteger.valueOf(0)
which returns BigInteger.ZERO when argument is 0
By using cached values, it is likely to yield significantly better space and time performance.
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.
精彩评论