Java Math

In Java, the java.lang.Math class provides a set of methods that allow you to perform mathematical operations. These methods are static, which means you can call them directly on the Math class without creating an instance of it.

Here are some commonly used methods in the Math class:

abs(): returns the absolute value of a number.

int num = -5;
int absNum = Math.abs(num); // absNum will be 5

max(): returns the greater of two numbers.

int num1 = 5;
int num2 = 10;
int maxNum = Math.max(num1, num2); // maxNum will be 10

min(): returns the lesser of two numbers.

int num1 = 5;
int num2 = 10;
int minNum = Math.min(num1, num2); // minNum will be 5

sqrt(): returns the square root of a number.

double num = 25;
double sqrtNum = Math.sqrt(num); // sqrtNum will be 5.0

pow(): returns the value of a number raised to a power.

double base = 2;
double exponent = 3;
double result = Math.pow(base, exponent); // result will be 8.0

round(): returns the closest integer to a number.

double num = 5.6;
int roundedNum = Math.round(num); //
Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial