Java Math Class
The java.lang.Math class is a utility class that provides various mathematical functions and constants, such as basic arithmetic operations, rounding functions, and the values of mathematical constants like PI and E. It is a final class, which means that it cannot be subclassed. You can use the Math class to perform mathematical operations in your Java programs, such as calculating the circumference of a circle or finding the exponent of a number. You can learn more about the Math class and the functions it provides in the Java documentation.
Java Math
The java.lang.Math class is a utility class that provides various mathematical functions and constants. It is a final class, which means that it cannot be subclassed.
Here are some examples of the functions provided by the Math class:
- Basic arithmetic operations:
addExact,subtractExact,multiplyExact,incrementExact,decrementExact,negateExact,abs,max,min,toIntExact, etc. - Rounding functions:
round,ceil,floor,rint,nextUp,nextDown, etc. - Trigonometric functions:
sin,cos,tan,asin,acos,atan,atan2, etc. - Exponential and logarithmic functions:
exp,log,log10,log1p,expm1, etc. - Power and square root functions:
pow,sqrt,cbrt,hypot, etc.
The Math class also provides the following constants:
PI: The mathematical constant pi.E: The mathematical constant e.
Here is an example of how you can use the Math class in a Java program:
public class Main {
public static void main(String[] args) {
// calculate the circumference of a circle with radius 5
double radius = 5;
double circumference = 2 * Math.PI * radius; // circumference is 31.41592653589793
// find the exponent of 2 to the power of 3
double exponent = 3;
double result = Math.pow(2, exponent); // result is 8.0
}
}Java Math Class Methods With Example
The java.lang.Math class includes several methods for performing basic mathematical operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Here are some examples:
abs(x): Returns the absolute value ofx. For example,abs(-1)returns1.max(x, y): Returns the greater of twointvalues. For example,max(1, 2)returns2.min(x, y): Returns the smaller of twointvalues. For example,min(1, 2)returns1.sqrt(x): Returns the square root ofx. For example,sqrt(9)returns3.0.pow(x, y): Returns the value ofxraised to the power ofy. For example,pow(2, 3)returns8.0.log(x): Returns the natural logarithm (basee) ofx. For example,log(Math.E)returns1.0.sin(x),cos(x),tan(x): Returns the sine, cosine, and tangent ofx, respectively.xis assumed to be in radians.toDegrees(x),toRadians(x): Convertsxfrom degrees to radians and from radians to degrees, respectively.
Basic Math Operations
| Method | Description |
|---|---|
abs(x) | Returns the absolute value of x. |
max(x, y) | Returns the greater of two int values. |
min(x, y) | Returns the smaller of two int values. |
pow(x, y) | Returns the value of x raised to the power of y. |
sqrt(x) | Returns the square root of x. |
Trigonometry
| Method | Description |
|---|---|
sin(x) | Returns the sine of x. |
cos(x) | Returns the cosine of x. |
tan(x) | Returns the tangent of x. |
Exponentials and Logarithms
| Method | Description |
|---|---|
exp(x) | Returns the value of e raised to the power of x. |
log(x) | Returns the natural logarithm (base e) of x. |
log10(x) | Returns the logarithm (base 10) of x. |
Rounding
| Method | Description |
|---|---|
ceil(x) | Returns the smallest (closest to negative infinity) double value that is greater than or equal to x and is equal to a mathematical integer. |
floor(x) | Returns the largest (closest to positive infinity) double value that is less than or equal to x and is equal to a mathematical integer. |
rint(x) | Returns the closest int to x. |
round(x) | Returns the closest long to x. |
Conversion
| Method | Description |
|---|---|
toDegrees(x) | Converts x from radians to degrees. |
toRadians(x) | Converts x from degrees to radians. |
