Roman
Roman

Reputation: 66156

What is the most effective way to create BigInteger instance from int value?

I have a method (in 3rd-party library) with BigInteger parameter:

public void setValue (BigInteger value) { ... }

I don't need 'all its power', I only need to work with integers. So, how can I pass integers to this method? My solution is to get string value from int value and then create BigInteger from string:

int i = 123;
setValue (new BigInteger ("" + i));

Are there any other (recommended) ways to do that?

Upvotes: 19

Views: 36557

Answers (4)

Paul Croarkin
Paul Croarkin

Reputation: 14675

setValue(BigInteger.valueOf(123L));

Upvotes: 3

Bozhidar Batsov
Bozhidar Batsov

Reputation: 56595

Use the static method BigInteger.valueOf(long number). int values will be promoted to long automatically.

Upvotes: 8

Petar Minchev
Petar Minchev

Reputation: 47373

You can use this static method: BigInteger.valueOf(long val)

Upvotes: 4

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

BigInteger.valueOf(i);

Upvotes: 53

Related Questions