ComputersProgramming

BigInteger Java: working with large numbers

The Java language is mainly used to write large enterprise web applications. However, it is also applicable to other subject areas where it is often necessary 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 - an overflow of type, in which the output beyond the allowed values leads to incorrect calculations. For this, there is a special class called BigInteger. 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 into an object of this type, you can use one of the overloads of its constructors, or the static valueOf method. The constructor can take a string or an array of bytes as an argument. To convert simple types that represent integers, use the static valueOf method in BigInteger Java.

Because Java does not have the ability to overload operators, there are methods to perform mathematical operations on encapsulated values in a given class. They will be discussed further on.

Java BigInteger: methods and examples of their use

This class has in its arsenal of many methods that allow you to manipulate numerical values and perform a variety of mathematical operations on them. Below is a list of these methods.

Let's analyze the methods that perform arithmetic operations:

- add (value) - adds the values of the current object, with the passed as argument;

- subtract (subtractor) - subtracts subtractor from the current value;

- multiply (value) - performs multiplication;

- divide (divider) - divides the current value into a divider;

- pow (int arg) - raises the value of the called object to a power of magnitude in arg;

- abs () - returns the absolute value of the called object;

- negate () - returns a BigInteger object whose value has the opposite sign.

Let's examine 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 ();

Notice the way objects are created. The variable val1 was initialized using the constructor that received the string, and stores the value 5. val2 is 10 as a result of the static valueOf method. The value of the adding variable is the result of adding the first two variables and is 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. In this case, type overflows are eliminated, since security in Java has one of the highest priorities, naturally without compromising functionality.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.atomiyme.com. Theme powered by WordPress.