Java Math Functions with Example
Java provides a built-in Math
class, part of the java.lang
package, that contains methods for performing basic numeric operations like exponentiation, rounding, trigonometry, square roots, and random number generation. The Math
class methods are static, so you can use them without creating an instance of the Math
class.
1. Basic Math Methods
Some of the most common mathematical methods in Java include:
Math.max()
: Returns the greater of two values.Math.min()
: Returns the smaller of two values.Math.abs()
: Returns the absolute value of a number.Math.sqrt()
: Returns the square root of a number.Math.pow()
: Returns the value of one number raised to the power of another.Math.round()
: Rounds a floating-point value to the nearest integer.
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("Max: " + Math.max(a, b)); // Outputs 20
System.out.println("Min: " + Math.min(a, b)); // Outputs 10
System.out.println("Absolute: " + Math.abs(-10)); // Outputs 10
System.out.println("Square Root: " + Math.sqrt(25));// Outputs 5.0
System.out.println("Power: " + Math.pow(2, 3)); // Outputs 8.0
System.out.println("Round: " + Math.round(7.6)); // Outputs 8
}
}
2. Random Numbers
Java provides the Math.random()
method to generate random numbers. This method returns a random double
value between 0.0
(inclusive) and 1.0
(exclusive). To generate a random number within a specific range, you can scale and shift the result of Math.random()
.
public class Main {
public static void main(String[] args) {
// Generate a random number between 0.0 and 1.0
double randomValue = Math.random();
System.out.println("Random Value: " + randomValue);
// Generate a random number between 0 and 100
int randomInt = (int) (Math.random() * 101);
System.out.println("Random Integer (0-100): " + randomInt);
}
}
In this example:
- The first random number is a floating-point number between 0.0 and 1.0.
- The second random number is an integer between 0 and 100.
3. Trigonometric Functions
The Math
class includes methods for trigonometric operations such as sine, cosine, and tangent. These methods operate with angles expressed in radians.
Math.sin()
: Returns the sine of the specified angle (in radians).Math.cos()
: Returns the cosine of the specified angle (in radians).Math.tan()
: Returns the tangent of the specified angle (in radians).
public class Main {
public static void main(String[] args) {
double radians = Math.PI / 4; // 45 degrees in radians
System.out.println("Sine: " + Math.sin(radians)); // Outputs 0.7071
System.out.println("Cosine: " + Math.cos(radians));// Outputs 0.7071
System.out.println("Tangent: " + Math.tan(radians));// Outputs 1.0
}
}
4. Rounding Functions
The Math
class offers several methods to round floating-point numbers to integers:
Math.ceil()
: Rounds a number up to the nearest integer.Math.floor()
: Rounds a number down to the nearest integer.Math.round()
: Rounds a number to the nearest integer, using standard rounding rules.
Example:
public class Main {
public static void main(String[] args) {
double value = 7.4;
System.out.println("Ceiling: " + Math.ceil(value)); // Outputs 8.0
System.out.println("Floor: " + Math.floor(value)); // Outputs 7.0
System.out.println("Round: " + Math.round(value)); // Outputs 7
}
}