Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialise BigInteger after creating instantces (constructor can't be called)

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?

like image 460
Bharat Gaikwad Avatar asked Jul 03 '11 13:07

Bharat Gaikwad


People also ask

Which of the following is a correct way of initializing a BigInteger variable *?

The simpler "proper" way would be String.

How do you initialize a large int in Java?

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.


2 Answers

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.

like image 167
Hovercraft Full Of Eels Avatar answered Sep 21 '22 08:09

Hovercraft Full Of Eels


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.

like image 21
ncmathsadist Avatar answered Sep 19 '22 08:09

ncmathsadist