Java Math Class
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: 4System.out.println(Math.abs(4));
OTUPUT: 4System.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
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.