Introduction
The Math class in Java provides static methods for various mathematical operations, including arithmetic, trigonometry, logarithms, and more. It is part of the java.lang package and cannot be instantiated.
Table of Contents
- What is
Math? - Common Methods
- Examples of
MathMethods - Conclusion
1. What is Math?
Math is a utility class that offers methods for mathematical functions, constants, and calculations.
2. Common Methods
abs(double a): Returns the absolute value of a number.max(int a, int b): Returns the greater of two values.min(int a, int b): Returns the smaller of two values.pow(double a, double b): Returns the value ofaraised to the power ofb.sqrt(double a): Returns the square root of a number.cbrt(double a): Returns the cube root of a number.ceil(double a): Returns the smallest integer greater than or equal toa.floor(double a): Returns the largest integer less than or equal toa.round(double a): Returns the closest long to the argument.exp(double a): Returnseraised to the power ofa.log(double a): Returns the natural logarithm of a number.log10(double a): Returns the base 10 logarithm of a number.sin(double a),cos(double a),tan(double a): Trigonometric functions.asin(double a),acos(double a),atan(double a): Inverse trigonometric functions.sinh(double a),cosh(double a),tanh(double a): Hyperbolic functions.random(): Returns a double value greater than or equal to0.0and less than1.0.toRadians(double angdeg): Converts an angle from degrees to radians.toDegrees(double angrad): Converts an angle from radians to degrees.
3. Examples of Math Methods
Example 1: Absolute Value
public class MathAbsExample {
public static void main(String[] args) {
int a = -10;
System.out.println("Absolute value of " + a + " is: " + Math.abs(a));
}
}
Output:
Absolute value of -10 is: 10
Example 2: Maximum and Minimum
public class MathMaxMinExample {
public static void main(String[] args) {
int a = 5, b = 10;
System.out.println("Max of " + a + " and " + b + ": " + Math.max(a, b));
System.out.println("Min of " + a + " and " + b + ": " + Math.min(a, b));
}
}
Output:
Max of 5 and 10: 10
Min of 5 and 10: 5
Example 3: Power and Square Root
public class MathPowSqrtExample {
public static void main(String[] args) {
double base = 2.0, exponent = 3.0;
System.out.println(base + " raised to " + exponent + " is: " + Math.pow(base, exponent));
System.out.println("Square root of " + base + " is: " + Math.sqrt(base));
}
}
Output:
2.0 raised to 3.0 is: 8.0
Square root of 2.0 is: 1.4142135623730951
Example 4: Trigonometric Functions
public class MathTrigExample {
public static void main(String[] args) {
double angle = Math.PI / 2;
System.out.println("Sin of PI/2: " + Math.sin(angle));
System.out.println("Cos of PI/2: " + Math.cos(angle));
System.out.println("Tan of PI/2: " + Math.tan(angle));
}
}
Output:
Sin of PI/2: 1.0
Cos of PI/2: 6.123233995736766E-17
Tan of PI/2: 1.633123935319537E16
Example 5: Rounding Functions
public class MathRoundingExample {
public static void main(String[] args) {
double number = 2.7;
System.out.println("Ceil of " + number + ": " + Math.ceil(number));
System.out.println("Floor of " + number + ": " + Math.floor(number));
System.out.println("Round of " + number + ": " + Math.round(number));
}
}
Output:
Ceil of 2.7: 3.0
Floor of 2.7: 2.0
Round of 2.7: 3
Example 6: Random Number Generation
public class MathRandomExample {
public static void main(String[] args) {
double randomValue = Math.random();
System.out.println("Random value: " + randomValue);
}
}
Output:
Random value: 0.42756136439384396
4. Conclusion
The Math class in Java provides a wide range of methods for mathematical operations. It is a valuable utility for performing calculations, conversions, and more in Java programming.