The Wayback Machine - https://web.archive.org/web/20210913201245/https://www.geeksforgeeks.org/java-math-abs-method-examples/
Skip to content
Related Articles

Related Articles

Improve Article

Java Math abs() method with Examples

  • Difficulty Level : Easy
  • Last Updated : 27 Aug, 2021

In order to compute the absolute value for any number we do have a specified method in Java referred to as abs() present inside java.lang.Math class. Absolute value refers to the positive value corresponding to the number passed as in arguments. Now you should be wondering what exactly it means so by this it is referred no matter what be it positive or negative number been passed for computation, the computation will occur over the positive corresponding number in both cases. 

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)

Parameters: Int, long, float, or double value whose absolute value is to be determined

Returns type: This method returns the absolute value of the argument.



Note:

  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.

Example 1:

Java




// Java Program to Illustrate Absolute Method of Math Class
 
// Importing all Math classes from java.util package
import java.lang.Math;
 
// Main class
class GFG {
   
    // Main driver method
    public static void main(String[] args)
    {
 
        // Custom integer input received from user
        int n = -7;
 
        // Printing value before applying absolute function
        System.out.println(
            "Without applying Math.abs() method : " + n);
 
        // Applying absolute math function and
        // storing it in integer variable
        int value = Math.abs(n);
 
        // Printing value after applying absolute function
        System.out.println(
            "With applying Math.abs() method : " + value);
    }
}
Output
Without applying Math.abs() method : -7
With applying Math.abs() method : 7

Example 2:

java




// Java program to Demonstrate Working
// of java.lang.Math.abs() method
 
// Importing Math class from java.lang package
import java.lang.Math;
 
// Main class
class Gfg {
 
    // Main driver method
    public static void main(String args[])
    {
        // Customly declaring and initializing all
        // arguments that ans() function takes
 
        // Float
        float a = 123.0f;
        float b = -34.2323f;
 
        // Double
        double c = -0.0;
        double d = -999.3456;
 
        // Integer
        int e = -123;
        int f = -0;
 
        // Long
        long g = -12345678;
        long h = 98765433;
 
        // abs() method taking float type as input
        System.out.println(Math.abs(a));
        System.out.println(Math.abs(b));
 
        // abs() method taking double type as input
        System.out.println(Math.abs(1.0 / 0));
        System.out.println(Math.abs(c));
        System.out.println(Math.abs(d));
 
        // abs() method taking int type as input
        System.out.println(Math.abs(e));
        System.out.println(Math.abs(f));
        System.out.println(Math.abs(Integer.MIN_VALUE));
 
        // abs() method taking long type as input
        System.out.println(Math.abs(g));
        System.out.println(Math.abs(h));
        System.out.println(Math.abs(Long.MIN_VALUE));
    }
}
Output
123.0
34.2323
Infinity
0.0
999.3456
123
0
-2147483648
12345678
98765433
-9223372036854775808

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 :