Difference Between Long and BigInteger in Java
Last Updated :
24 Oct, 2023
In Java, long and BigInteger are two different data types used to represent and perform operations on the large integers. In this article we will learn about BigInteger and Long in Java.
long in Java
long is a primitive data type in Java used to store 64-bit signed integers.
- It has a range of values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
- long is a fixed-size data type meaning it has a fixed amount of the memory allocated for each variable.
The long data type is commonly used for integer values that are expected to be larger than the range of int.
Syntax:
long variableName;
Example of Java long
Below is the implementation of the long in Java are mentioned below:
Java
// Java Program to implement Long
import java.io.*;
// Driver Class
public class LongExample {
// main function
public static void main(String[] args) {
// declaring and initializing a long variable
long num1 = 1234567890L;
// performing arithmetic operations with long values
long num2 = 9876543210L;
long sum = num1 + num2;
long difference = num2 - num1;
long product = num1 * num2;
long quotient = num2 / num1;
// printing the results
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
System.out.println("sum = " + sum);
System.out.println("difference = " + difference);
System.out.println("product = " + product);
System.out.println("quotient = " + quotient);
}
}
Outputnum1 = 1234567890
num2 = 9876543210
sum = 11111111100
difference = 8641975320
product = -6253480962446024716
quotient = 8
BigInteger in Java
BigInteger is a class in Java's java.math package that represents arbitrary-precision integers.
- It can handle integers of the practically unlimited size constrained only by available memory.
- The BigInteger objects are not fixed in size and they dynamically allocate memory as needed.
Syntax :
BigInteger num1 = new BigInteger();
Examples of BigInteger
Below is the implementation of the above method:
Java
import java.io.*;
import java.math.BigInteger;
public class BigIntegerExample {
public static void main(String[] args) {
// creating BigInteger objects
BigInteger num1 = new BigInteger("12345678901234567890");
BigInteger num2 = new BigInteger("98765432109876543210");
// performing arithmetic operations with BigInteger objects
BigInteger sum = num1.add(num2);
BigInteger difference = num2.subtract(num1);
BigInteger product = num1.multiply(num2);
BigInteger quotient = num2.divide(num1);
// printing the results
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
System.out.println("sum = " + sum);
System.out.println("difference = " + difference);
System.out.println("product = " + product);
System.out.println("quotient = " + quotient);
}
}
Outputnum1 = 12345678901234567890
num2 = 98765432109876543210
sum = 111111111011111111100
difference = 86419753208641975320
product = 1219326311370217952237463801111263526900
quotient = 8
Difference between long and BigInteger
Differences between long and BigInterger are mentioned in the table given below:
|
Primitive
| Class
|
Fixed (64 bits)
| Dynamic
|
Limited
| Practically unlimited
|
Limited by size
| Unlimited
|
Fixed
| Dynamic
|
Wraps around
| Does not wrap and handles overflow gracefully
|
Suitable for the common integer needs
| The Suitable for very large integers and specialized arithmetic operations
|
Generally faster due to hardware-level support
| Slower due to dynamic allocation and method calls
|