rtperson's answer is correct from a Java perspective, but in Scala you can do more with scala.BigInt
s than what you can do with java.math.BigInteger
s.
For example:
scala> val a = new BigInteger("26525285981219105863630848482795");
a: java.math.BigInteger = 26525285981219105863630848482795
scala> a + a
:7: error: type mismatch;
found : java.math.BigInteger
required: String
a + a
The canonical way in Scala to instantiate a class is to use a factory located in the
companion object. When you write Foo(args) in Scala, this is translated to Foo.apply(args), where Foo is a singleton object - the companion object. So to find the ways of constructing BigInt
s you could have a look at the BigInt
object in the Scala library, and specifically at its apply
construct.
So, three ways of constructing an BigInt
are: passing it an Int
, a Long
or a String
to parse. Example:
scala> val x = BigInt(12)
x: BigInt = 12
scala> val y = BigInt(12331453151315353L)
y: BigInt = 12331453151315353
scala> val z = BigInt("12124120474210912741099712094127124112432")
z: BigInt = 12124120474210912741099712094127124112432
scala> x + y * z
res1: BigInt = 149508023628635151923723925873960750738836935643459768508
Note the nice thing that you can do natural looking arithmetic operations with a BigInt
, which is not possible with a BigInteger
!