Java Math Class

Imran Khan
1 min readSep 21, 2022

Java.lang.Math package in java provides Math class. Math class provides us some of the predefined method such as min, max, sqrt etc.

Below are the examples of some of the methods from Math class we use in our day-to-day life:

max() method returns the bigger number passed in the parameter.

System.out.println(Math.max(5,3));OUTPUT: 5

min() method returns the smaller number passed in the parameter.

System.out.println(Math.min(5,3));

OUTPUT: 3

sqrt() method will provide us the square root of the number passed in argument.

System.out.println(Math.sqrt(4));

OTUPUT: 2.0

random() method returns the random number in between 0 and 1.

System.out.println(Math.random());OTUPUT: 0.8685304957692445

abs() method returns the absolute positive of any negative number. If in case we are passing it as 0, it will return 0 as a result.

System.out.println(Math.abs(-4));

OTUPUT: 4
System.out.println(Math.abs(4));

OTUPUT: 4
System.out.println(Math.abs(0));

OTUPUT: 0

floor() method returns the floor value of an input. This will return closest/nearest positive value.

System.out.println(Math.floor(-1.2));

OTUPUT: -2.0
System.out.println(Math.floor(1.2));OTUPUT: 2.0

I hope you found out this article interesting and informative. Please share it with your friends to spread the knowledge.

You can follow me for upcoming blogs follow.
Thank you!

--

--