Java Math - Complete Guide
Introduction to Java Math
Java provides the Math class in the java.lang package, which contains methods for performing standard numeric operations such as exponentiation, logarithms, square roots, and trigonometric functions.
Since the Math class methods are static, you can call them directly without creating an object (for example, Math.sqrt(25)).
Basic Math Methods
Some commonly used methods in the Math class include abs(), max(), min(), sqrt(), pow(), and random(). These methods work with different numeric types like int, long, float, and double.
public class MathDemo {
public static void main(String[] args) {
int a = -10;
int b = 20;
System.out.println("Absolute value of a: " + Math.abs(a));
System.out.println("Maximum: " + Math.max(a, b));
System.out.println("Minimum: " + Math.min(a, b));
System.out.println("Square root of 16: " + Math.sqrt(16));
System.out.println("2 raised to 3: " + Math.pow(2, 3));
// Random number between 0.0 and 1.0
System.out.println("Random number: " + Math.random());
}
}
Absolute value of a: 10 Maximum: 20 Minimum: -10 Square root of 16: 4.0 2 raised to 3: 8.0 Random number: 0.123456789 (varies each run)
Rounding Methods
Java provides methods for rounding decimals: Math.round(), Math.ceil(), and Math.floor().
- Math.round() rounds to the nearest integer (returns long for double inputs).
- Math.ceil() returns the smallest integer greater than or equal to the value.
- Math.floor() returns the largest integer less than or equal to the value.
public class RoundingDemo {
public static void main(String[] args) {
double x = 5.65;
double y = -5.65;
System.out.println("Round x: " + Math.round(x));
System.out.println("Ceil x: " + Math.ceil(x));
System.out.println("Floor x: " + Math.floor(x));
System.out.println("Ceil y: " + Math.ceil(y));
System.out.println("Floor y: " + Math.floor(y));
}
}
Round x: 6 Ceil x: 6.0 Floor x: 5.0 Ceil y: -5.0 Floor y: -6.0
Trigonometric Methods
The Math class provides trigonometric functions such as sin(), cos(), and tan(). These methods take input in radians.
Use Math.toRadians() to convert degrees to radians before passing values to trigonometric methods.
public class TrigonometryDemo {
public static void main(String[] args) {
double radians = Math.toRadians(30); // Convert 30 degrees to radians
System.out.println("sin(30°): " + Math.sin(radians));
System.out.println("cos(30°): " + Math.cos(radians));
System.out.println("tan(30°): " + Math.tan(radians));
}
}
sin(30°): 0.49999999999999994 cos(30°): 0.8660254037844387 tan(30°): 0.5773502691896257
Best Practices for Math
- ✅ Use Math.random() for quick random values, but prefer java.util.Random or java.security.SecureRandom for more control or security
- ✅ Convert degrees to radians before using trigonometric functions
- ✅ Use Math.abs() to ensure non-negative values where required
- ✅ For precise decimal calculations (e.g., financial applications), prefer BigDecimal over floating-point Math methods
- ✅ Remember Math methods are static—no need to create a Math object