Imagine an instance of BigInteger
, then how to initialize it after creating instance?
For example:
BigInteger t = new BigInteger();
How to put a value in t
?
If the constructor cannot be called, then what can be done, to put the value in the object?
The simpler "proper" way would be String.
To convert a long (or a regular integer) to BigInteger, use the static factory method valueOf. The call BigInteger. valueOf(<i>someInteger</i>) returns a new BigInteger object holding the integer value you specify.
I'm not 100% sure of what specifically is confusing you as you'd initialize the items in the BigInteger array as you would any other object array. e.g.,
BigInteger t2 [] = new BigInteger[2];
t2[0] = new BigInteger("2");
t2[1] = BigInteger.ZERO; // ZERO, ONE, and TEN are defined by constants
// or
BigInteger[] t3 = {new BigInteger("2"), BigInteger.ZERO};
Edit 1:
Ah, now I understand your problem: you want to create a BigInteger instance and then later set its value. The answer is the same as for Strings: you can't, and that it is because BigIntegers like Strings are immutable and can't be changed once created. For this reason the class has no "setter" methods. The way to change the value of a BigInteger variable is to set it to a new BigInteger instance.
To convert a long (or a regular integer) to BigInteger, use the static factory method valueOf. The call BigInteger.valueOf(<i>someInteger</i>)
returns a new BigInteger
object holding the integer value you specify. You could also use new BigInteger("" + <i>someInteger</i>)
to get the same thing, but this is clunkier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With