The Java Math class provides many methods to perform numeric operations like exponential, logarithm, square root, and trigonometric equations.
Math.methodName().
The java.lang.Math class is a final class that contains methods for performing basic numeric operations. Since it belongs to the java.lang package, it is imported automatically in every Java program.
Key Characteristics:
Math.PI and Math.E.Instead of hardcoding values like 3.14, Java provides built-in constants for better precision:
Java provides multiple ways to round numbers, and choosing the right one is crucial:
Rounds up to the nearest integer. (e.g., 2.1 becomes 3.0)
Rounds down to the nearest integer. (e.g., 2.9 becomes 2.0)
Standard rounding. Rounds to the nearest whole number. (e.g., 2.5 becomes 3)
These are the most common methods you will use in daily programming:
| Method | Input Example | Result |
|---|---|---|
abs(-10) |
Negative integer | 10 |
sqrt(64) |
Perfect square | 8.0 |
pow(2, 3) |
Base and Exponent | 8.0 (2 raised to power 3) |
The Math class is a utility powerhouse. Whether you are building a simple calculator or a complex physics engine, these methods provide the speed and accuracy needed for calculations.
Next: Learn Java Booleans →