Java Math Class in the java.lang Package
Math Class
The Math class provides methods related to mathematics, such as geometry, trigonometry, and floating-point calculations.
Because the Math class is a final class, it cannot be inherited.
The constructor of the Math class is declared with the private access modifier, so instances cannot be created, and all member variables and methods are declared as static.
Main Math Example
The following program is an example using methods of the Math class.
package com.devkuma.basic.math;
public class Mathematics {
public static void main(String[] args) {
System.out.println("abs(10.6) :" + Math.abs(10.6));
System.out.println("ceil(10.3) : " + Math.ceil(10.3));
System.out.println("floor(10.3) : " + Math.floor(10.3));
System.out.println("max(3, 5) : " + Math.max(3, 5));
System.out.println("min(3, 5) : " + Math.min(3, 5));
System.out.println("random() : " + Math.random());
System.out.println("round(10.5) : " + Math.round(10.5));
System.out.println("rint(10.3) : " + Math.rint(10.3));
}
}
Execution result:
abs(10.6) :10.6
ceil(10.3) : 11.0
floor(10.3) : 10.0
max(3, 5) : 5
min(3, 5) : 3
random() : 0.3319476750319902
round(10.5) : 11
rint(10.3) : 10.0
Math.signum
This introduces how to get the sign of a numeric value with Math.
package com.devkuma.basic.math;
public class MathSignum {
public static void main(String[] args) {
double plus = Math.signum(100); // 1.0
double zero = Math.signum(0); // 0.0
double minus = Math.signum(-100); // -1.0
System.out.println(plus);
System.out.println(zero);
System.out.println(minus);
}
}
Execution result:
1
0
-1
| Input value | signum() return value |
|---|---|
| 100 (positive) | 1 |
| 0 (zero) | 0 |
| -100 (negative) | -1 |
Math Class Variables
| Variable | Description |
|---|---|
E |
Natural logarithm constant value (e: 2.718…) |
PI |
Pi value (3.14…) |
Main Math Methods
| Method | Description |
|---|---|
static int abs(int a) |
Returns the absolute value of a as int. |
static long abs(long a) |
Returns the absolute value of a as long. |
static float abs(float a) |
Returns the absolute value of a as float. |
static double abs(double a) |
Returns the absolute value of a as double. |
static double ceil(double a) |
Returns the value obtained by rounding up the fractional part of a. |
static double floor(double a) |
Returns the value obtained by truncating the fractional part of a. |
static int max(int a, int b) |
Returns the larger of a and b as int. |
static long max(long a, long b) |
Returns the larger of a and b as long. |
static float max(float a, float b) |
Returns the larger of a and b as float. |
static double max(double a, double b) |
Returns the larger of a and b as double. |
static int min(int a, int b) |
Returns the smaller of a and b as int. |
static long min(long a, long b) |
Returns the smaller of a and b as long. |
static float min(float a, float b) |
Returns the smaller of a and b as float. |
static double min(double a, double b) |
Returns the smaller of a and b as double. |
static double random() |
Returns a random value. |
static long round(double a) |
Returns the value obtained by rounding the fractional part of a. |
static double rint(double a) |
Returns the integer closest to a. |
static double sin(double a) |
Returns the sine value of a. |
static double cos(double a) |
Returns the cosine value of a. |
static double tan(double a) |
Returns the tangent value of a. |
static double sqrt(double a) |
Returns the square root value of a. |