简体   繁体   中英

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?

BigInteger.valueOf(i);

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

您可以使用此静态方法: BigInteger.valueOf(long val)

setValue(BigInteger.valueOf(123L));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM