Java Math Class
The Math class in Java provides a collection of static methods for
performing common
mathematical operations such as finding maximum/minimum values, powers,
square roots,
rounding, and generating random numbers. Since all methods are static,
you can call them
directly using Math.methodName() without creating an object.
This guide explains the most commonly used Java Math methods with fresh
examples and
practical use cases.
Finding Maximum and Minimum Values
Math.max(a, b)
Returns the greater of two values.
public class MaxExample {
public static void main(String[] args) {
int temperatureCityA = 32;
int temperatureCityB = 28;
int highestTemp = Math.max(temperatureCityA,
temperatureCityB);
System.out.println("Highest temperature: " +
highestTemp);
}
}
Output
Highest temperature: 32
Math.min(a, b)
Returns the smaller of two values.
public class MinExample {
public static void main(String[] args) {
double priceProductA = 499.99;
double priceProductB = 549.50;
double lowestPrice = Math.min(priceProductA,
priceProductB);
System.out.println("Lowest price: " +
lowestPrice);
}
}
Square Root Calculation
Math.sqrt(x)
Returns the square root of a number.
public class SquareRootExample {
public static void main(String[] args) {
double area = 144.0;
double sideLength =
Math.sqrt(area);
System.out.println("Side length of square: " +
sideLength);
}
}
Absolute Value
Math.abs(x)
Returns the positive (absolute) value.
public class AbsoluteExample {
public static void main(String[] args) {
int bankBalance = -3500;
int positiveBalance =
Math.abs(bankBalance);
System.out.println("Absolute value: " +
positiveBalance);
}
}
Power and Exponents
Math.pow(base, exponent)
Returns base raised to the power exponent.
public class PowerExample {
public static void main(String[] args) {
double principal = 1000;
double rate = 1.05;
int years = 3;
double amount = principal * Math.pow(rate,
years);
System.out.println("Compound amount: " +
amount);
}
}
Key Notes
- Math.pow() always returns a double
- Even whole-number results appear with .0
- Example: Math.pow(2, 8) → 256.0
Example
public class RoundingExample {
public static void main(String[] args) {
double value = 7.35;
long nearest =
Math.round(value);
double up = Math.ceil(value);
double down =
Math.floor(value);
System.out.println("Rounded: " +
nearest);
System.out.println("Ceiling: " +
up);
System.out.println("Floor: " +
down);
}
}
Generating Random Numbers
Math.random()
Returns a random double between 0.0 (inclusive) and 1.0
(exclusive).
Random Integer Within a Range
To generate numbers in a specific range, scale and cast the result.
Random number from 1 to 100
public class RandomRangeExample {
public static void main(String[] args) {
int min = 1;
int max = 100;
int randomNumber = (int)(Math.random() *
(max - min + 1)) + min;
System.out.println("Random number: " +
randomNumber);
}
}
Formula Explained
(Math.random() * (max - min + 1)) + min
When to Use the Java Math Class
Use the Math class when you need:
- Mathematical calculations
- Geometry formulas
- Financial computations
- Random values
- Scientific operations
- Data normalization