GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java Math Class - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java Math Class

Last Updated : 23 Jul, 2025

Java.lang.Math Class methods help to perform numeric operations like square, square root, cube, cube root, exponential and trigonometric operations.

Declaration

public final class Math
   extends Object

Methods of Math Class in Java

Math class consists of methods that can perform mathematical operations and can make long calculations a bit easier. Let us check the method provided in the Math class.

MethodDescription
sinReturns the trigonometric value of the sine of an angle.
cosReturns the trigonometric value of the cosine of an angle.
tanReturns the trigonometric value of the tangent of an angle.
asinReturns the trigonometric value of the arc sine of an angle.
acosReturns the trigonometric value of the arc cosine of an angle.
atanReturns the trigonometric value of the arc tangent of an angle.
toRadiansConvert value in degrees to value in radians
toDegreesConvert value in radians to value in degrees
expReturns Euler's number e raised to the power of a double value
logReturns the natural logarithm (base e) of a double value
log10Returns the base 10 logarithms of a double value
sqrtReturns the correct rounded positive square root of a double value
cbrtReturns the cube root of a double value
IEEEremainderComputes the remainder operation on two arguments as instructed by the IEEE 754 standard.
ceilReturns the smallest double value that is greater than or equal to the argument. It is a fixed mathematical integer
floorReturns the smallest double value that is less than or equal to the argument. It is a fixed mathematical integer.
rintReturns the double value that is closest in value to the argument and is equal to a mathematical integer
atan2Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta)
powReturns(pow(a,b)) the value of ab.
roundReturns the closest int to the argument, with ties rounding to positive infinity
randomReturns a double value with a positive sign, in the range [0.0, 1.0).
addExactReturns the sum of its arguments, throwing an exception if the result overflows a value.
subtractExactReturns the difference of its arguments, throwing an exception if the result overflows a value.
multiplyExactReturns the product of the arguments, throwing an exception if the result overflows a value.
incrementExactReturns the argument value incremented by 1 and throws an exception if the value overflows.
decrementExactReturns the argument value decremented by 1 and throws an exception if the value overflows.
negateExactReturns the negation of the argument and throws an exception if the value overflows.
toIntExactReturns the value of the long argument and throws an exception if the int overflows.
floorDivReturns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient
floorModReturns the floor modulus of the arguments
absReturn the absolute value
maxReturns the maximum out of the two arguments
minReturns the minimum out of the two arguments
ulpReturns the size of an ulp of the argument.
signumReturns value 0,1 and -1 depending upon if the argument is equal, greater, or less than zero respectively.
sinhReturns the hyperbolic sine of the value.
coshReturns the hyperbolic cosine of the value.
tanhReturns the hyperbolic sine of the value.
hypotReturns the hyperbolic tangent of the value.
expm1Returns ex-1
log1pReturns the natural logarithm of the sum of the argument and 1
copySignReturns the first value argument with the sign of the second value argument
getExponentReturns the unbiased exponent used in the representation of a value
nextAfterReturns the floating-point number adjacent to the first argument in the direction of the second argument.
nextUpReturns the floating-point value adjacent to d in the direction of positive infinity.
nextDownReturns the floating-point value adjacent to d in the direction of negative infinity.
scalbReturns f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiplied to a member of the value set.

Example of the Java Math Class

Java
// Java program to demonstrate the
// Use of Math Class
public class MathLibraryExample {
    public static void main(String[] args) {
        int i = 7;
        int j = -9;
        double x = 72.3;
        double y = 0.34;

        System.out.println("i is " + i);
        System.out.println("j is " + j);

        // The absolute value of a number is equal to the
        // number if the number is positive or zero and
        // equal to the negative of the number if the number
        // is negative.

        System.out.println("|" + i + "| is " + Math.abs(i));
        System.out.println("|" + j + "| is " + Math.abs(j));

        // Truncating and Rounding functions
        // You can round off a floating point number to the
        // nearest integer with round()
        System.out.println(x + " is approximately " + Math.round(x));
        System.out.println(y + " is approximately " + Math.round(y));

        // The "ceiling" of a number is the smallest integer
        // greater than or equal to the number. Every
        // integer is its own ceiling.
        System.out.println("The ceiling of " + x + " is " + Math.ceil(x));
        System.out.println("The ceiling of " + y + " is " + Math.ceil(y));

        // The "floor" of a number is the largest integer
        // less than or equal to the number. Every integer
        // is its own floor.
        System.out.println("The floor of " + x + " is " + Math.floor(x));
        System.out.println("The floor of " + y + " is " + Math.floor(y));

        // Comparison operators

        // min() returns the smaller of the two arguments
        // you pass it
        System.out.println("min(" + i + "," + j + ") is " + Math.min(i, j));
        System.out.println("min(" + x + "," + y + ") is " + Math.min(x, y));

        // There's a corresponding max() method
        // that returns the larger of two numbers
        System.out.println("max(" + i + "," + j + ") is " + Math.max(i, j));
        System.out.println("max(" + x + "," + y + ") is " + Math.max(x, y));

        // The Math library defines a couple of useful
        // constants:
        System.out.println("Pi is " + Math.PI);
        System.out.println("e is " + Math.E);

        // Trigonometric methods. All arguments are given in
        // radians
        // Convert a 45 degree angle to radians
        double angle = 45.0 * 2.0 * Math.PI / 360.0;
        System.out.println("cos(" + angle + ") is " + Math.cos(angle));
        System.out.println("sin(" + angle + ") is " + Math.sin(angle));

        // Inverse Trigonometric methods. All values are
        // returned as radians
        double value = 0.707;
        System.out.println("acos(" + value + ") is " + Math.acos(value));
        System.out.println("asin(" + value + ") is " + Math.asin(value));
        System.out.println("atan(" + value + ") is " + Math.atan(value));

        // Exponential and Logarithmic Methods
        // exp(a) returns e (2.71828...) raised
        // to the power of a.
        System.out.println("exp(1.0) is " + Math.exp(1.0));
        System.out.println("exp(10.0) is " + Math.exp(10.0));
        System.out.println("exp(0.0) is " + Math.exp(0.0));

        // log(a) returns the natural
        // logarithm (base e) of a.
        System.out.println("log(1.0) is " + Math.log(1.0));
        System.out.println("log(10.0) is " + Math.log(10.0));
        System.out.println("log(Math.E) is " + Math.log(Math.E));

        // pow(x, y) returns x raised to the power of y.
        System.out.println("pow(2.0, 2.0) is " + Math.pow(2.0, 2.0));
        System.out.println("pow(10.0, 3.5) is " + Math.pow(10.0, 3.5));
        System.out.println("pow(8, -1) is " + Math.pow(8, -1));

        // sqrt(x) returns the square root of x.
        for (i = 0; i < 10; i++) {
            System.out.println("The square root of " + i + " is " + Math.sqrt(i));
        }

        // Finally there's one Random method
        // that returns a pseudo-random number
        // between 0.0 and 1.0;
        System.out.println("Here's one random number: " + Math.random());
        System.out.println("Here's another random number: " + Math.random());
    }
}

Output
i is 7
j is -9
|7| is 7
|-9| is 9
72.3 is approximately 72
0.34 is approximately 0
The ceiling of 72.3 is 73.0
The ceiling of 0.34 is 1.0
The floor of 72.3 is 72.0
The floor of 0.34 is 0.0
min(7,-9) i...

NaN's argument: A constant holding a Not-a-Number (NaN) value of type double. It is equivalent to the value returned by Double.longBitsToDouble(0x7ff8000000000000L).

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.