Math Class in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The Math class in Java includes methods for executing fundamental mathematical operations such as square root, logarithm, exponential, and trigonometric functions.

In this article, we will be reading about the Math class in Java and the various methods defined in it.

Introduction

Various techniques for working with numerical calculations are offered by the Java Math class, including min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), and abs() methods.

Only when a certain minimum or maximum value is present, does overflow occur. When performing mathematical operations such as incrementing, decrementing, dividing, finding absolute values, or negating numbers, it's crucial to ensure they stay within the appropriate maximum and minimum boundaries.

When using the addExact(), subtractExact(), multiplyExact(), and toIntExact() methods, an ArithmeticException is thrown if the resultant value exceeds the allowable range for the data type (int or long).

Declaration of Math Class in Java

The declaration for the Math class in Java is as follows:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Field of Math Class in Java

Following are the fields for the Math class in Java

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Static Double E

This is the decimal number that sits closest to the value of 'e,' the base used in natural logarithms.

Static Double PI

This double value contains the value of π, i.e. the ratio of the circle's circumference to its diameter.

Methods of Math Class in Java

The Math class in Java contains numerous methods to perform various kinds of math operations including basic math, trigonometric, logarithmic, operations. Let's have a look at some of these.

Basic Math Methods

NameDescription
Math.signum()Returns the sign of the value
Math.abs() Returns absolute value of the value in the parentheses
Math.copySign()Returns the absolute value of the first argument along with the sign in the second argument
Math.round()Returns value rounded to the nearest value
Math.max()Returns largest of the two values
Math.min()Returns minimum of the 2 values
Math.ceil()Returns the smallest integer greater than or equal to the value in the parentheses
Math.floor()Returns the largest integer smaller than or equal to the value in the parentheses
Math.pow()Returns the value of the first argument raised to the power given in the second argument
Math.sqrt()Returns square root of the number
Math.cbrt()Returns cube root of the number
Math.random()Returns a positive double value between 0.0 and 1.0
Math.hypot()Returns the hypotenuse value (i.e. square root of the sum of x squared and y squared) without intermediate overflow or underflow.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Logarithmic Math Methods

NameDescription
Math.log()Returns the natural logarithm of a double value
Math.log10()Returns the base 10 logarithm of a double value.
Math.log1p()Returns the natural logarithm of the sum of the argument and 1.
Math.exp()Returns E (Euler's Number = 2.71828) raised to power of a double value
Math.expm1()Returns power of E after subtracting one from it

Trigonometric Math Methods

NameDescription
Math.sin()Returns sine value of the number
Math.cos()Returns cosine value of the number
Math.tan()Returns tangent value of the number
Math.asin()Returns arc sine value of the number
Math.acos()Returns arc cosine value of the number
Math.atan()Returns arc tangent value of the number

Hyperbolic Math Methods

NameDescription
Math.sinh()Returns hyperbolic sine value of the number
Math.cosh()Returns hyperbolic cosine value of the number
Math.tanh()Returns hyperbolic tangent value of the number

Angular Math Methods

NameDescription
Math.toDegreesConverts radian angle to angle in degrees
Math.toRadiansConverts degrees angle to radian angle

Examples of Math Class in Java

Example 1: A few basic mathematic operations

Output:

Example 2: A few logarithmic operations

Output:

Conclusion

  • The Math class in Java provides methods that are used to perform numerical computations including basic mathematical, logarithmic, exponential, trigonometric, hyperbolic, angular, etc.
  • The Math class in Java has 2 fields - static double E and static double PI which store approximate values of e and π respectively.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more