Java Math Class

Java.lang.Math package provides Math class. It 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          

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan