BigInteger of Java large number operation [original]

In Java, there are many digital processing classes, such as integer class. However, the integer class has certain limitations. Let's take a look at the BigInteger class, which is more powerful than the integer class.

The number range of BigInteger type is much larger than that of integer type. We all know that integer is a wrapper class of int, and the maximum value of int is 231-1. If you want to calculate a larger number, you can't use the integer data type. Therefore, BigInteger class is provided in Java to process a larger number. BigInteger supports integers of any precision, that is, in the operation, BigInteger type can accurately represent integer values of any size without losing any information.

A variety of operations are encapsulated in the BigInteger class! In addition to the basic addition, subtraction, multiplication and division operations, it also provides operations such as absolute value, opposite number, maximum common divisor and judging whether it is a prime number.

Using the BigInteger class, you can instantiate a BigInteger object and automatically call the corresponding constructor. The BigInteger class has many constructors, but the most direct way is that the parameters represent the number to be processed in string form.

The syntax is as follows:

Where Val is a decimal string.

If you convert 2 to BigInteger type, you can use the following statement for initialization:

Once an object instance is created, you can call some methods in BigInteger class for operation operations, including basic mathematical operations and bit operations, as well as some operations such as taking opposite numbers and absolute values. The following are several common operation methods of BigInteger class.

Here is an example. Create a class in the project, create an instance object of BigInteger class in the main method of the class, call various methods of the object to realize addition, subtraction, multiplication, division and other operations of large integers, and output the running results.

The above is the detailed explanation and example analysis of BigInteger class. If you like, please continue to pay attention to programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>