Skip to content
Related Articles

Related Articles

Improve Article
Java Math abs() method with Examples
  • Difficulty Level : Basic
  • Last Updated : 10 Jan, 2020

The java.lang.Math.abs() returns the absolute value of a given argument.

  • If the argument is not negative, the argument is returned.
  • If the argument is negative, the negation of the argument is returned.

Syntax :

public static DataType abs(DataType a)
a : the argument whose absolute value is to be determined
Returns : This method returns the absolute value of the argument.
  1. If the argument is of double or float type:
    • If the argument is positive zero or negative zero, the result is positive zero.
    • If the argument is infinite, the result is positive infinity.
    • If the argument is NaN, the result is NaN.
  2. If the argument is of int or long type:
    • If the argument is equal to the value of Integer.MIN_VALUE or Long.MIN_VALUE, the most negative
      representable int or long value, the result is that same value, which is negative.

// Java program to demonstrate working
// of java.lang.Math.abs() method
import java.lang.Math;
class Gfg {
    // driver code
    public static void main(String args[])
        float a = 123.0f;
        float b = -34.2323f;
        double c = -0.0;
        double d = -999.3456;
        int e = -123;
        int f = -0;
        long g = -12345678;
        long h = 98765433;
        // abs() method taking float type as input
        // abs() method taking double type as input
        System.out.println(Math.abs(1.0 / 0));
        // abs() method taking int type as input
        // abs() method taking long type as input






Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :