JDK-6407464 : BigInteger should support Autoboxing.
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 6
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2006-04-03
  • Updated: 2011-10-31
  • Resolved: 2011-10-31
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
Here is a list of operations that Integers support with autoboxing, and how they would be translated for a BigInteger (all operations return BigInteger):

BigInteger a, b;
++b, b++ ... b = b.add(BigInteger.ONE)
--b, b-- ... b = b.subtract(BigInteger.ONE)
-b ... b.negate()
+b ... b
~b ... b.not()

a+b ... a.add(b)
a-b ... a.subtract(b)
a*b ... a.multiply(b)
a/b ... a.divide(b)
a%b ... a.remainder(b) // note that a.mod(b) would not work here

a>>x ... a.shiftRight(x)
a<<x ... a.shiftLeft(x)
a>>>x ... (not implemented in the BigInteger class)
a&b ... a.and(b)
a|b ... a.or(b)
a^b ... a.xor(b)

a>b ... a.compareTo(b) > 0
a<b ... a.compareTo(b) < 0
a<=b ... a.compareTo(b) <= 0
a>=b ... a.compareTo(b) >= 0
a==b ... a.compareTo(b) == 0
a!=b ... a.compareTo(b) != 0

a=b ... a = b
a+=b ... a = a.add(b)
a-=b ... a = a.subtract(b)
a*=b ... a = a.multiply(b)
a/=b ... a = a.divide(b)
a%=b ... a = a.remainder(b) // note that a.mod(b) would not work here
a>>=x ... a = a.shiftRight(x)
a<<=x ... a = a.shiftLeft(x)
a>>>=x ... (not implemented in the BigInteger class)
a&=b ... a = a.and(b)
a|=b ... a = a.or(b)
a^=b ... a = a.xor(b)

Most if not all of these operations are also supported by BigDecimal, though its inability to store repeating decimals lends it to be less useful (though this is a topic for another day).

JUSTIFICATION :
BigInteger is a very popular class when it comes to fast and precise integer calculations. I use it both on the job and during programming competitions, where a great deal of time saved by using BigInteger instead of messing with integer or bit arrays. Still, much more time could be saved if BigInteger were to support autoboxing.

BigInteger is not simply a mathematical construct like a matrix or a vector that may or may not be worth risking operator overloading. For the most part, it behaves like the Integer, Long, Float, and the other Autoboxed variables in many ways (including being derived from the same class, the Number class). I'm not asking for operator overloading, rather I am asking only to be able to work with the built-in BigInteger class in the same way I work with the Integer and Long classes..

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
See the executable section.
ACTUAL -
See the executable section.

---------- BEGIN SOURCE ----------
// Code that looks like:

BigInteger a = BigInteger.valueOf(300)
BigInteger b = BigInteger.valueOf(300).pow(a.mutlitply(BigInteger.valueOf(11)).add(BigInteger.valueOf(13)).divide(Big.Integer.valueOf(17));

// Would be replaced with code that looks like:

/*
BigInteger a = 300;
BigInteger b = (a.pow(300) * 11 + 13 ) / 17;
*/

/*
The former is impossible to read. I'm likely to get a compile error or two in that line of code. The latter is simple, easy to read, nearly impossible to generate a compile error accidently, and much more appropriate to what BigInteger really is ... a precise Integer.
*/
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I have not found a built in >>> operation in the BigInteger class and I have been unable to find a workaround for that operation. I am beginning to believe that such an operation would not make sense anyway. There are still the 32 other operations that do make sense!

Comments
EVALUATION This is a reasonable request to add a limited set of additional operators to the language to ease development with BigInteger (and BigDecimal, though it's not mentioned here). It is different from supporting general operator overloading on user-defined classes, as in C++. (See 4905919)
14-11-2006