Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is Math Class in Java and How to use it?

Published on Aug 28,2019 4.7K Views

38 / 72 Blog from Java Core Concepts

Java Math Class in Java contains certain methods to perform different numeric operations, for instance, exponential, square root, logarithmic, trigonometric functions. Let us dig deeper and understand the concept in detail.

Let’s begin!

What is Java math class?

To perform different numeric calculations in Java, math class has provided several methods which you will study further.
Some of the methods are min(), max(), avg(), sin(), cos() etc.

Now let us study the methods in an elaborate manner.

 

What are the Java math methods?

Below is a list describing several methods that Java math class offers:

Math.min(): this method returns the smallest of two values.
Math.max(): this method returns the largest of two values
Math.abs(): this method returns the absolute value of the value provided.
Math.round(): this method is used to round off the decimal numbers to the nearest value.
Math.sqrt(): this method returns the square root of a number given.
Math.cbrt(): this method returns the cube root of a given number.
Math.floor(): this method is used to find the largest integer value which is less than or equal to the argument and is equal to the double value of a mathematical integer.
Math.pow(): this method returns the value of the first argument raised to the power second argument provided.
Math.ceil(): list method is used to find the smallest integer value that is greater than or equal to the argument.
Math.floorDiv(): this method is used to find the largest integer value which is less than or equal to the quotient.
Math.random(): this java random method returns a double value that carries a positive sign which is greater than or equal to 0.0 and less than 1.0
Math.rint(): this method returns the double value that is closest to the given argument.
Math.ulp(): this method returns the size of the ULP of the argument.
Math.addExact(): this method is used to return the sum of its arguments and throws an exception if the result overflows an integer or a long value.
Math.subtractExact(): this method returns the difference of the arguments provided and throws an exception if the result overflows and an integer value.
Math.multiplyExact(): this method returns the product of the arguments and throws an exception if the result overflows and integer or long value.
Math.incrementExact(): this method returns the argument which is incremented by one and throws an exception if the result overflows and an integer value.
Math.decrementExact(): this method returns the argument that is decremented by one and throws an exception if the result overflows and integer or long value.
Math.negateExact(): this method returns the negation of the argument and throws an exception if the result overflows and integer or long value.

These were the few methods for performing basic numerical operations. Now let us move forward and understand the concept of logarithmic math methods.

Logarithmic math methods

Here is a list explaining these methods:
Math.log(): this method is used to return the natural logarithm of double value
Math.log10(): this method is used to return the base 10 logarithm of a double value
Math.exp(): this method returns E (Euler’s value) raised to the power of a double value.
Math.log1p(): this method returns the natural logarithm of the sum of the argument and also one.
Math.expm1(): this method calculates the power of Euler’s number and subtracts 1 from it.

These are some of the logarithmic math methods that help you to ease down your calculation path while using Java math class.

The next topic queued up in this tutorial bears the title trigonometric math methods.

Trigonometric math methods

Below mentioned is a list of these methods:
Math.sin(): this method returns the sine value of a given double value.
Math.asin(): this method returns the arc sin value of a given double value
Math.cos(): this method returns the cos value of a given double value.
Math.acos: this method returns the arc cosine value of the given double value.
Math.tan(): this method returns the tangent value of a given double value.
Math.atan(): this method returns the arc tangent value of a given double value.

The next segment consists of angular math methods.

Angular math methods

There are two angular math methods explained below:
Math.toRadians: this method is used to convert the specified degrees angle to the angle measuring in radians.
Math.toDegrees: this method is used to convert the radians angle to an equivalent angle measuring in degrees.

Next we have hyperbolic math methods.

Hyperbolic math methods

This segment consists of three methods mentioned below:
Math.sinh(): this method is used to return the trigonometric hyperbolic sine value of a double value.
Math.cosh(): this method is used to return the trigonometric hyperbolic cosine value of a double value.
Math.tanh(): this method is used to return the trigonometric hyperbolic tangent value of a double value.

I hope the concept of hyperbolic math methods is clear to you by now.

Now let me show you an example of using these methods in a Java program:

Java program showing the usage of Java math class methods:

public class JavaMathExample1{
      public static void main(String[] args){
          double x = 28;
          double y = 4;
          System.out.println("Maximum number of x and y is: " +Math.max(x, y));
          System.out.println("Square root of y is: " + Math.sqrt(y));
          System.out.println("Power of x and y is: " + Math.pow(x, y));
          System.out.println("Logarithm of x is: " + Math.log(x));
          System.out.println("Logarithm of y is: " + Math.log(y));
          System.out.println("log10 of x is: " + Math.log10(x));
          System.out.println("log10 of y is: " + Math.log10(y));
          System.out.println("log1p of x is: " +Math.log1p(x));
          System.out.println("exp of a is: " +Math.exp(x));
          System.out.println("expm1 of a is: " +Math.expm1(x));
      }
}

Output:

Maximum number of x and y is: 28.0
Square root of y is: 2.0
Power of x and y is: 614656.0
Logarithm of x is: 3.332204510175204
Logarithm of y is: 1.3862943611198906
log10 of x is: 1.4471580313422192
log10 of y is: 0.6020599913279624
log1p of x is: 3.367295829986474
exp of a is: 1.446257064291475E12
expm1 of a is: 1.446257064290475E12

With this, we have reached towards the end of this tutorial. I hope the contents explained the above-added value to your Java knowledge. We will keep exploring the Java world. Stay tuned!

If you find any queries related to this article, please write them down in the comment section below and we shall revert you back soon.

Now that you have understood basics of Java, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Math Class in Java and How to use it?

edureka.co