The Math Class is available in java.lang pakcage. The Math Class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric methods etc.
1. Absolute Method(Double) in Math Class
public class AbsoluteDouble {public static void main(String[] args) {
double x = 4876.1874d;
double y = -9789.434d;
System.out.println("Math.abs(" + x + ") = " + Math.abs(x));
System.out.println("Math.abs(" + y + ") = " + Math.abs(y));
}
}
Output :
Math.abs(4876.1874) = 4876.1874Math.abs(-9789.434) = 9789.434
2. Absolute Method(Float) in Math Class
public class AbsoluteFloat {public static void main(String[] args) {
float x = 1.2345f;
float y = -857.54f;
System.out.println("Math.abs(" + x + ") = " + Math.abs(x));
System.out.println("Math.abs(" + y + ") = " + Math.abs(y));
}
}
Output :
Math.abs(1.2345) = 1.2345Math.abs(-857.54) = 857.54
3. Absolute Method(Int) in Math Class
public class AbsoluteInt {public static void main(String[] args) {
int x = 4876;
int y = -9789;
System.out.println("Math.abs(" + x + ") = " + Math.abs(x));
System.out.println("Math.abs(" + y + ") = " + Math.abs(y));
}
}
Output :
Math.abs(4876) = 4876Math.abs(-9789) = 9789
4. Cube Root Method in Math Class
public class CubeRoot { public static void main(String[] args) {double x = 125;
double y = 216;
System.out.println("Math.cbrt(" + x + ")=" + Math.cbrt(x));
System.out.println("Math.cbrt(" + y + ")=" + Math.cbrt(y));
}
}
Output :
Math.abs(125) = 5.0Math.abs(216) = 6.0
5. Ceil Method in Math Class
public class Ceil {public static void main(String[] args) {
double x = 125.9; double y = 0.4873;
System.out.println("Math.ceil(" + x + ")=" + Math.ceil(x));
System.out.println("Math.ceil(" + y + ")=" + Math.ceil(y));
}
}
Output :
Math.ceil(125.9) = 126.0Math.ceil(0.4873) = 1.0
6. Floor Method in Math Class
public class Floor {public static void main(String[] args) {
double x = 60984.1; double y = -497.99;
System.out.println("Math.floor(" + x + ")=" + Math.floor(x));
System.out.println("Math.floor(" + y + ")=" + Math.floor(y));
}
}
Output :
Math.floor(60984.1) = 60984.0Math.floor(-497.99) = -498.0
7. Cosine Method in Math Class
public class Cosine {public static void main(String[] args) {
double x = 45.0;
double y = 180.0;
x = Math.toRadians(x);
y = Math.toRadians(y);
System.out.println("Math.cos(" + x + ")=" + Math.cos(x));
System.out.println("Math.cos(" + y + ")=" + Math.cos(y));
}
}
Output :
Math.cos(0.7853981633974483)=0.7071067811865476Math.cos(3.141592653589793)=-1.0
7. Expression Method in Math Class
public class Expression {public static void main(String[] args) {
double x = 5;
double y = 0.5;
System.out.println("Math.exp(" + x + ")=" + Math.exp(x));
System.out.println("Math.exp(" + y + ")=" + Math.exp(y));
}
}
Output :
Math.exp(5) = 148.4131591025766Math.exp(0.5) = 1.6487212707001282
8. Logarithm Method in Math Class
public class Logarithm {public static void main(String[] args) {
double x = 60984.1;
double y = -497.99;
System.out.println("Math.log(" + x + ")=" + Math.log(x));
System.out.println("Math.log(" + y + ")=" + Math.log(y));
}
}
Output :
Math.log(60984.1) = 11.018368453441132Math.log(-497.99) = NaN
9. Logarithm with Base 10 Method in Math Class
public class LogBaseTen {public static void main(String[] args) {
double x = 60984.1;
double y = 1000;
System.out.println("Math.log10(" + x + ")=" + Math.log10(x));
System.out.println("Math.log10(" + y + ")=" + Math.log10(y));
}
}
Output :
Math.log10(60984.1) = 4.78521661890635Math.log10(1000) = 3.0
10. Maximum Method (Double) in Math Class
public class MaxDouble{public static void main(String[] args) {
double x = 60984.1;
double y = 1000;
System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x,y));
}
}
Output :
Math.max(60984.1, 1000) = 60984.111. Maximum Method (Float) in Math Class
public class MaxFloat{public static void main(String[] args) {
float x = 60984.1f;
float y = 1000f;
System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x,y));
}
}
Output :
Math.max(60984.1, 1000) = 60984.112. Maximum Method (Int) in Math Class
public class MaxInt{public static void main(String[] args) {
int x = 60984;
int y = 1000;
System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x,y));
}
}
Output :
Math.max(60984, 1000) = 6098413. Maximum Method (Long) in Math Class
public class MaxLong{public static void main(String[] args) {
long x = 6098499785l;
long y = 100087898745;
System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x,y));
}
}
Output :
Math.max(6098499785l, 100087898745) = 10008789874514. Minumum Method (Double) in Math Class
public class MinDouble{public static void main(String[] args) {
double x = 60984.1;
double y = 1000;
System.out.println("Math.min(" + x + "," + y + ")=" + Math.min(x,y));
}
}
Output :
Math.min(60984.1, 1000) = 100015. Minumum Method (Float) in Math Class
public class MinFloat {public static void main(String[] args) {
float x = 60984.1f;
float y = 1000f;
System.out.println("Math.min(" + x + "," + y + ")=" + Math.min(x,y));
}
}
Output :
Math.min(60984.1, 1000) = 100016. Minumum Method (Int) in Math Class
public class MinInt{public static void main(String[] args) {
int x = 60984;
int y = 1000;
System.out.println("Math.min(" + x + "," + y + ")=" + Math.min(x,y));
}
}
Output :
Math.min(60984, 1000) = 100017. Minumum Method (Long) in Math Class
public class MinLong{public static void main(String[] args) {
long x = 6098499785l;
long y = 100087898745;
System.out.println("Math.min(" + x + "," + y + ")=" + Math.min(x,y));
}
}
Output :
Math.min(6098499785l, 100087898745) = 6098499785l18. Power Method in Math Class
public class Power {public static void main(String[] args) {
double x = 2.0;
double y = 5.4;
System.out.println("Math.pow(" + x + "," + y + ")=" + Math.pow(x,y));
System.out.println("Math.pow(" + y + "," + x + ")=" + Math.pow(y,x));
}
}
Output :
Math.pow(2.0, 5.4) = 42.22425314473263Math.pow(5.4, 2.0) = 29.160000000000004
19. Round Method (Double) in Math Class
public class RoundDouble {public static void main(String[] args) {
double x = 1654.9874;
double y = -9765.134;
System.out.println("Math.round(" + x + ")=" + Math.round(x));
System.out.println("Math.round(" + y + ")=" + Math.round(y));
}
}
Output :
Math.round(1654.9874)=1655Math.round(-9765.134)=-9765
20. Round Method (Float) in Math Class
public class RoundFloat {public static void main(String[] args) {
float x = 1654.9874f;
float y = -9765.134f;
System.out.println("Math.round(" + x + ")=" + Math.round(x));
System.out.println("Math.round(" + y + ")=" + Math.round(y));
}
}
Output :
Math.round(1654.9874f)=1655Math.round(-9765.134f)=-9765
21. Sine Method in Math Class
public class Sine {public static void main(String[] args) {
double x = 45;
double y = -180;
x = Math.toRadians(x);
y = Math.toRadians(y);
System.out.println("Math.sin(" + x + ")=" + Math.sin(x));
System.out.println("Math.sin(" + y + ")=" + Math.sin(y));
}
}
Output :
Math.sin(0.7853981633974483)=0.7071067811865475Math.sin(-3.141592653589793)=-1.2246467991473532E-16
22. Tangent Method (Float) in Math Class
public class Tangent {public static void main(String[] args) {
double x = 45;
double y = -180;
x = Math.toRadians(x);
y = Math.toRadians(y);
System.out.println("Math.tan(" + x + ")=" + Math.tan(x));
System.out.println("Math.tan(" + y + ")=" + Math.tan(y));
}
}
Output :
Math.tan(0.7853981633974483)=0.9999999999999999Math.tan(-3.141592653589793)=1.2246467991473532E-16
23. Square Root Method in Math Class
public class SquareRoot {public static void main(String[] args) {
double x = 9;
double y = 25;
System.out.println("Math.sqrt(" + x + ")=" + Math.sqrt(x));
System.out.println("Math.sqrt(" + y + ")=" + Math.sqrt(y));
}
}
Output :
Math.sqrt(9) = 3.0Math.sqrt(25) = 5.0