The Java language is mainly used for writing large corporate web applications. However, it is also applicable to other subject areas, where quite often there is a need to work with very large numbers. Although Java has primitive types, there are situations where they are not enough.
It is known that all primitives have one unpleasant feature - type overflow, in which going beyond the permissible values leads to incorrect calculations. There is a special BigInteger class for this. Java would not be Java if it did not have a separate class with functionality for this case. Let's consider it in more detail.
BigInteger Java Class: Description
As you already understood, the BigInteger class serves as a wrapper for large integer values. To encapsulate a number in an object of this type, you can use one of the overloads of its constructors, or the static method valueOf. The constructor can take a string or an array of bytes as an argument. BigInteger Java uses the static valueOf method to convert simple types representing integers.
Since there is no possibility of operator overloading in Java, corresponding methods are provided for performing mathematical operations on values encapsulated in this class. They will be discussed further.
Java BigInteger: methods and examples of their use
This class has in its arsenal many methods that allow you to manipulate numerical values and perform various mathematical operations on them. The following is a list of these methods.
We will analyze the methods that perform arithmetic operations:
- add (value) - performs the addition of the values of the current object, with the passed as an argument;
- subtract (subtractor) - performs subtractor subtractor from the current value;
- multiply (value) - performs multiplication;
- divide (divider) - performs the division of the current value by divider;
- pow (int arg) - raises the value of the calling object to a power, the value in arg;
- abs () - returns the absolute value of the caller;
- negate () - returns a BigInteger object whose value has the opposite sign.
Let's look at a simple example of performing an arithmetic operation on two values encapsulated in an object:
- BigInteger val1, val2, adding, dividing, oppositeSign;
- val1 = new BigInteger (“5”);
- val2 = BigInteger.valueOf (10);
- adding = val1.add (val2);
- dividing = val2.divide (val1);
- oppositeSign = val1.negate ();
Pay attention to the way objects are created. The variable val1 was initialized using the constructor that took the string and stores the value 5. val2 has a value of 10 as a result of the static method valueOf. The value of the adding variable is the result of adding the first two variables and is equal to 15. The variable dividing accordingly stores the result of the subtraction. oppositeSign is equal to the value of val1 with the opposite sign, i.e. -5.
Conclusion
As you can see, the BigInteger Java class provides various tools for operations on very large numbers. At the same time, type overflow is excluded, since security in Java has one of the highest priorities, naturally without compromising functionality.