Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, the BigInteger
and BigDecimal
classes are used for performing operations on large numbers or numbers with high precision. BigInteger
is used for arbitrary-precision integers, while BigDecimal
is used for arbitrary-precision floating-point numbers.
In this tutorial, we will go through examples of using the BigInteger
and BigDecimal
classes for big number operations.
To use BigInteger
, import the java.math.BigInteger
class:
import java.math.BigInteger;
Performing operations with BigInteger
:
BigInteger bigInteger1 = new BigInteger("12345678901234567890"); BigInteger bigInteger2 = new BigInteger("98765432109876543210"); // Addition BigInteger sum = bigInteger1.add(bigInteger2); System.out.println("Sum: " + sum); // Subtraction BigInteger difference = bigInteger1.subtract(bigInteger2); System.out.println("Difference: " + difference); // Multiplication BigInteger product = bigInteger1.multiply(bigInteger2); System.out.println("Product: " + product); // Division BigInteger quotient = bigInteger2.divide(bigInteger1); System.out.println("Quotient: " + quotient); // Remainder BigInteger remainder = bigInteger2.remainder(bigInteger1); System.out.println("Remainder: " + remainder);
To use BigDecimal
, import the java.math.BigDecimal
class:
import java.math.BigDecimal;
Performing operations with BigDecimal
:
BigDecimal bigDecimal1 = new BigDecimal("12345.67890123456789"); BigDecimal bigDecimal2 = new BigDecimal("98765.43210987654321"); // Addition BigDecimal sum = bigDecimal1.add(bigDecimal2); System.out.println("Sum: " + sum); // Subtraction BigDecimal difference = bigDecimal1.subtract(bigDecimal2); System.out.println("Difference: " + difference); // Multiplication BigDecimal product = bigDecimal1.multiply(bigDecimal2); System.out.println("Product: " + product); // Division (use scale and rounding mode to avoid non-terminating decimal expansion) BigDecimal quotient = bigDecimal1.divide(bigDecimal2, 10, RoundingMode.HALF_UP); System.out.println("Quotient: " + quotient); // Remainder BigDecimal remainder = bigDecimal1.remainder(bigDecimal2); System.out.println("Remainder: " + remainder);
When using the BigDecimal
class, it's important to set the scale and rounding mode when performing division to avoid non-terminating decimal expansion, which could cause an ArithmeticException
. In the example above, we set the scale to 10 and use RoundingMode.HALF_UP
to round the result.
Both BigInteger
and BigDecimal
classes provide many more methods for performing various mathematical operations and comparisons.
Arithmetic Operations with BigInteger in Java:
BigInteger
allows arithmetic operations on large integers.
BigInteger num1 = new BigInteger("12345678901234567890"); BigInteger num2 = new BigInteger("98765432109876543210"); BigInteger sum = num1.add(num2);
Working with Large Integers in Java:
BigInteger
is suitable for working with integers beyond the range of primitive types.
BigInteger largeInt = new BigInteger("123456789012345678901234567890");
Performing Decimal Arithmetic in Java with BigDecimal:
BigDecimal
is used for precise decimal arithmetic.
BigDecimal decimal1 = new BigDecimal("123.456"); BigDecimal decimal2 = new BigDecimal("789.012"); BigDecimal product = decimal1.multiply(decimal2);
Precision and Scale in Java BigDecimal:
BigDecimal
supports precision and scale for accurate decimal representation.
BigDecimal value = new BigDecimal("123.456"); int precision = value.precision(); int scale = value.scale();
Formatting BigDecimal Values in Java:
BigDecimal
values can be formatted for display.
BigDecimal amount = new BigDecimal("12345.6789"); String formattedAmount = amount.setScale(2, RoundingMode.HALF_UP).toString();
Rounding Methods in BigDecimal:
BigDecimal
provides various rounding methods.
BigDecimal value = new BigDecimal("123.456"); BigDecimal roundedValue = value.setScale(2, RoundingMode.HALF_UP);
Comparing BigDecimal Values in Java:
Compare BigDecimal
values for equality or ordering.
BigDecimal value1 = new BigDecimal("123.456"); BigDecimal value2 = new BigDecimal("789.012"); int comparison = value1.compareTo(value2);
Java BigInteger and BigDecimal Examples:
Examples demonstrate the use of BigInteger
and BigDecimal
in various scenarios.
BigInteger intVal = new BigInteger("12345678901234567890"); BigDecimal decVal = new BigDecimal("12345.6789");
BigInteger vs BigDecimal in Java:
Choose between BigInteger
and BigDecimal
based on the nature of the data.
BigInteger intVal = new BigInteger("12345678901234567890"); BigDecimal decVal = new BigDecimal("12345.6789");
Parsing Strings to Create BigInteger and BigDecimal Objects in Java:
Parse strings to create BigInteger
and BigDecimal
objects.
String intStr = "12345678901234567890"; String decStr = "12345.6789"; BigInteger intVal = new BigInteger(intStr); BigDecimal decVal = new BigDecimal(decStr);
Handling Overflow and Underflow with BigInteger in Java:
BigInteger
can handle large values without overflow.
BigInteger bigInt = new BigInteger("123456789012345678901234567890");
Common Use Cases for BigInteger and BigDecimal in Java:
Use BigInteger
for precise integer arithmetic and BigDecimal
for accurate decimal calculations.
BigInteger intVal = new BigInteger("12345678901234567890"); BigDecimal decVal = new BigDecimal("12345.6789");
Converting Between Primitive Types and BigInteger/BigDecimal in Java:
Convert between primitive types and BigInteger
or BigDecimal
.
BigInteger intVal = BigInteger.valueOf(1234567890L); BigDecimal decVal = BigDecimal.valueOf(123.45);
Java Math Operations with BigInteger and BigDecimal:
Perform standard math operations using BigInteger
and BigDecimal
.
BigInteger result = BigInteger.valueOf(2).pow(100); BigDecimal product = BigDecimal.valueOf(3.14).multiply(BigDecimal.valueOf(2.0));
Using BigInteger for Cryptographic Operations in Java:
BigInteger
is suitable for cryptographic operations.
BigInteger base = new BigInteger("12345678901234567890"); BigInteger exponent = new BigInteger("98765432109876543210"); BigInteger result = base.modPow(exponent, new BigInteger("123456789"));
Java BigDecimal for Financial Calculations:
BigDecimal
is commonly used for accurate financial calculations.
BigDecimal principal = new BigDecimal("1000.00"); BigDecimal rate = new BigDecimal("0.05"); BigDecimal interest = principal.multiply(rate);