When primitive data types can’t help you Java has some special classes to rescue you. One such condition arises when you’re dealing with large numbers which cannot be contained in Primitive data types such as int and long. One might use String data type to overcome this problem. But, if we want to perform arithmetic operations on the given number this method falls short. In this post, we will discuss how can we overcome this problem with the help of BigInteger in Java
Following pointers will be covered in this article,
So let us get started with this article,
BigInteger In Java
Classes and Objects
Before getting started with the BigInteger class it is essential to understand the fundamentals of Object Oriented Programming. Classes and objects form the core of OOPs. These are powerful concepts used frequently when dealing with languages that support OOPs. So. Let’s go through these concepts and develop a brief understanding of their working.
The main ideas behind OOPs are code modularity and code reusability. These ideas are implemented with the help of class and object. A class is a place where a user can set the behavior and properties of its instances. You can also consider a class as a prototype of the objects belonging to that car.
For example
Let’s consider a class named ‘Dog’. Now, if we think of a dog we can say that all dogs have some common properties such as color, legs, tail, ears, etc. and behaviors such as walking, shaking the tail, barking, etc. we can write all these common properties as attributes and behaviors as methods in our class ‘Dog’. Now, every instance of this class will have the same properties and behaviors. Now, we can create as many objects as we want and use the functionalities mentioned in the class. This makes the code modular and reusable.
Classes are also called user-defined data types and are used when the functionality that we are trying to achieve is not fulfilled by using primitive data types. BigInteger class is an example of that. Let’s study this example in and out.
Let us take a look at the next point of this BigInteger In Java
BigInteger Class
At this point, we have enough knowledge of classes and objects to get started with the BigInteger class provided by Java. BigInteger class is a part of Java.math package and comes with various constructors. While creating objects of this class to store big numbers there is no upper limit for the number that can be stored. The only limitation is the memory available.
BigInteger class should only be used if ineluctable. As it grabs more memory compared to the primitive data types and you cannot do arithmetic operations using respective arithmetic operator, as Java does not support operator overloading.
Declaration
BigInteger Big;
Initialization
BigInteger Big2 = new BigInteger("124321098456719538751253254");
In the next section, we will discuss the constructors and methods of this class.
Now we would understand what are constructors,
BigInteger In Java: Constructors
Let’s have a look at the various constructors provided by the class BigInteger. More the number of constructors available more the number of ways to create objects of class BigInteger. The important thing to keep in mind is that the constructors don’t have a return type and they do not return anything.
Sr.No | Constructors and Parameters | Description |
1 | BigInteger(byte[] val) | This constructor helps in translating a byte array containing the two’s-complement binary representation of a BigInteger into a BigInteger. |
2 | BigInteger(int signum, byte[] magnitude) | Helps in translating the sign-magnitude representation of a BigInteger into a BigInteger. |
3 | BigInteger(int bitLength, int certainty, Random rnd) | Helps in constructing a randomly generated positive BigInteger that is probably prime, with the specified bitLength. |
4 | BigInteger(String val) | Helps in converting the decimal String representation of a BigInteger into a BigInteger. |
5 | BigInteger(String val, int radix) | Converts the String representation of a BigInteger in the specified radix into a BigInteger. |
There are a total of 5 types of constructors and are used according to the situations and the parameters available with us. To understand the concept we will be using the 4th constructor from the table.
Next bit in this BigInteger In Java article is,
Methods of BigInteger Class
As we discussed earlier, every class has different attributes and methods which are common to all instances of that class. While doing arithmetic operations with the help of BigInteger class use of arithmetic operators is of no use. Hence to conduct arithmetic operations we need to use BigInteger class methods.
Method | The return type and value | Description |
abs( ) | This function returns a BigInteger whose value is the absolute value of this BigInteger. | This function helps in finding the absolute value of the given BigInteger |
add(BigInteger val) | This function returns a BigInteger whose value is (this + val). val- value that is to be added to this BigInteger | Helps in performing the addition operation |
compareTo(BigInteger val) | Returns -1 if BigInteger is numerically less compared to val, 0 if equal, 1 if greater. val- BigInteger to which this BigInteger is to be compared. | Compares this BigInteger with the specified BigInteger |
divide(BigInteger val) | This function returns a BigInteger whose value is (this / val). val- value by which BigInteger is to be divided. | Helps to perform division with given BigInteger and throws an exception if divided by 0 |
equals(Object x) | This function returns true if and only if the specified Object is a BigInteger whose value is numerically equal to this BigInteger. x – Object to which this BigInteger is to be compared. | Returns a boolean value. True if the specified Object is a BigInteger whose value is numerically equal to this BigInteger. |
intValue( ) | this BigInteger converted to an int type. | Converts this BigInteger to an int. |
multiply(BigInteger val) | This function returns a BigInteger whose value is (this * val). val- Value that is to be multiplied by this BigInteger | Helps in performing the Multiplication operation |
subtract(BigInteger val) | This function returns a BigInteger whose value is (this – val). val- value that is to be subtracted from this BigInteger | Helps in performing the Subtraction operation |
The methods mentioned in the above table are the methods which are used most of the time while dealing with the BigInteger class. To look at the complete list provided by the BigInteger class go through the official BigInteger class docs provided by Oracle.
Example
After understanding OOPs concepts, BigInteger class, its constructors, and methods, now let’s consider an example and demonstrate the use of BigInteger class.
Let’s find the time taken by light to reach the nearest galaxy to earth i.e 2.4011019Km.
import java.math.BigInteger; public class BiggInteger { public static void main(String[] args) { BigInteger Distance = new BigInteger("2401000000000000000000"); BigInteger SpeedofLight = new BigInteger("1080000000"); BigInteger Time = Distance.divide(SpeedofLight); System.out.println("Time taken by light to reach the Andromeda Galaxy is " + Time + " hours"); } }
Output:
Process finished with exit code = 0
Note- The above case is for example purpose. In reality, the answer may differ. Try different galaxies or stars and edit the code accordingly.
Thus we have come to an end of this article on ‘BigInteger in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this article and we will get back to you as soon as possible.