In java, we can handle basic arithmetical operations on numbers.Like simple addition, subtraction, multiplication and division. But additionally for executing complex mathematical operations, java provides Math class. The package java.lang.Math represents the Math class. You basically don't need to import this package to perform mathematical operations as the Math class in the java.lang package.
(java.lang package is automatically added to run your program in java and provides various fundamental classes like System, Integer, Object, String, Class, Exception etc. to run your programs.)
**Yet, if you want you can import the Math class externally.
One more thing should be added here that, all the attributes of Math class are static by default. Thus you can use them directly without creating objects.
Let's see few of the important attributes (PI only) and methods of the Math class to perform various mathematical tasks:
#1 Find out min and max number
There are methods min() and max() in the Math class to find out min and max number based on the provided number.
public class Main{
public static void main (String[] args) {
//find out minimum number
System.out.println(Math.min(10, 20));
//find out maximum number
System.out.println(Math.max(10, 20));
}
}
/**
* Output:
* 10
* 20
*/
It is okay if you provide variables as arguments.
public class Main{
public static void main (String[] args) {
int x = 10;
int y = 20
//find out minimum number
System.out.println(Math.min(x, y));
//find out maximum number
System.out.println(Math.max(x, y));
}
}
/**
* Output:
* 10
* 20
*/
Note: The number should be int, float, double and long either positive or negative.
#2 Work with Exponentiation (repeated multiplication or simply call it power)
How to return the value of x to the power of y
There is a method called pow(double, double) that returns the exponential (powered) value.
public class Main{
public static void main (String[] args) {
int x = 5;
int y = 3;
//find out powered value (3 power 2)
System.out.println(Math.pow(3, 2));
//returns the value of x to the power y (x power y )
System.out.println(Math.pow(x, y));
}
}
/**
* Output:
* 9.0
* 125.0
*/
Note: the Math.pow() returns double and required two arguments.
#3 Returns the random number between 0 and 1 (The alternative class is Random)
There is a method called Math.random() that returns random number between 0 and 1.
public class Main{
public static void main (String[] args) {
System.out.println(Math.random());
}
}
/**
* Output:
* 0.4053012513485045 (first compile)
* 0.703321370261342 (second compile)
*/
Note:Math.random() returns double value and accepts 0 arguments.
#3 Returns the nearest number
The Math.round() method of the Math class returns the value of x rounded to its nearest integer (down or up)
public class Main{
public static void main (String[] args)
{
//returns 4 as below .5
System.out.println(Math.round(4.3));
//returns 5 as up .5
System.out.println(Math.round(4.8));
}
}
/**
* Output:
* 4
* 5
*/
Note: The number should be float or double. Math.round() returns int and long.
#4 Find out the nearest rounded up number
There is a method called ceil() in Math class. It returns the value of x rounded up to it's nearest double value.
public class Main{
public static void main (String[] args) {
//nearest rounded up number
System.out.println(Math.ceil(5.3)); //returns 6.0
System.out.println(Math.ceil(5.8)); //returns 6.0
}
}
/**
* Output: (double value)
* 6.0
* 6.0
*/
Note:Math.ceil() method returns double value and the provided number could be positive or negative.
#5 Find out the nearest down number (positive or negative)
There is a method called floor() in Math class. It returns the value of x rounded down to it's nearest double value.
public class Main{
public static void main (String[] args) {
//nearest rounded up number
System.out.println(Math.ceil(5.3)); //returns 6.0
System.out.println(Math.ceil(5.8)); //returns 6.0
}
}
/**
* Output: (double value)
* 6.0
* 6.0
*/
Note:Math.floor() method returns double value and the provided number could be positive or negative.
#6 Find out the square root √ of given integer or double number
There is a method called sqrt(double) in the Math class. It returns the square root of the given number
public class Main{
public static void main (String[] args) {
//returns square root
System.out.println(Math.sqrt(16)); //returns 4.0
System.out.println(Math.sqrt(64)); //returns 8.0
}
}
/**
* Output: (double value)
* 4.0
* 8.0
*/
Note:Math.sqrt() method returns double and the number should be integer or double.
#7 Find out the cube root 3√ of given integer or double number
There is a method called cbrt(double) in the Math class. It returns the cube root of the given number
public class Main{
public static void main (String[] args) {
//returns cube root
System.out.println(Math.cbrt(8)); //returns 2.0
System.out.println(Math.cbrt(27)); //returns 3.0
System.out.println(Math.cbrt(64)); //returns 4.0
}
}
/**
* Output: (double value)
* 2.0
* 3.0
* 4.0
*/
Note:Math.sqrt() method returns double and the number should be integer or double.