AppDividend
Latest Code Tutorials

Java Math.abs Function: The Complete Guide

To find the absolute value of a given argument in Java, use the Math.abs() method. This is often helpful when the program deals with mathematical expressions where the |x| value (the absolute value of a variable x) is required to be calculated.

Java Math.abs

Java Math.abs() is a built-in method that returns an int value’s absolute (positive) value. The Math.abs() method gives the absolute value of the argument. The argument can be int, double, long, and float.

Syntax

public static int abs(int x)
public static long abs(long x)
public static float abs(float x)
public static double abs(double x)

Parameter

The variable x, whose absolute value is to be determined.

Return Value

If the argument is not negative, it returns the value of the argument as it is. Otherwise, it returns the negation of that value.

public static int abs(int x)

For an int argument, if the argument is non-negative, the abs() method returns the argument as it is. For negative arguments, it returns the negation of that value. However, if the argument is Integer.MIN_VALUE, which is the most negative representable int, returns the same negative value.

Consider the following example for an int argument.

import java.lang.Math;

public class Example1 {
  public static void main(String[] args) {
    int a = 24;
    int b = -36;
    int c = Integer.MIN_VALUE;

    System.out.println(Math.abs(a));
    System.out.println(Math.abs(b));
    System.out.println(Math.abs(c));
  }
}

Output

-> javac Example1.java
-> java Example1
24
36
-2147483648

public static long abs(long x)

For a long argument, if the argument is non-negative, the abs() method returns the argument as it is. For negative arguments, it returns the negation of that value. However, if the argument is Long.MIN_VALUE, which is the most negative representable long, returns the same negative value.

Consider the following example for a long argument.

import java.lang.Math;

public class Example2 {
  public static void main(String[] args) {
    long a = 24090911;
    long b = -360912340;
    long c = Long.MIN_VALUE;

    System.out.println(Math.abs(a));
    System.out.println(Math.abs(b));
    System.out.println(Math.abs(c));
  }
}

Output

-> javac Example2.java
-> java Example2
24090911
360912340
-9223372036854775808

public static float abs(float x)

For a float argument, if the argument is non-negative, the abs() method returns the argument as it is. For negative arguments, it returns the negation of that value.

For either positive zero or negative zero, it returns a positive zero. For infinite argument returns positive infinity. For NaN argument returns NaN.

Consider the following example for a float argument.

import java.lang.Math;

public class Example3 {
  public static void main(String[] args) {
    float a = 240.1f;
    float b = -36.40f;
    float c = -0.0f;

    System.out.println(Math.abs(a));
    System.out.println(Math.abs(b));
    System.out.println(Math.abs(c));
    System.out.println(Math.abs(6.4 / 0));
    System.out.println(Math.abs(6.4 % 0));
  }
}

Output

-> javac Example3.java
-> java Example3
240.1
36.4
0.0
Infinity
NaN

public static double abs(double x)

For a double argument, if the argument is non-negative, the abs() method returns the argument as it is. For negative arguments, it returns the negation of that value. For either positive zero or negative zero, it returns a positive zero. For infinite argument returns positive infinity. For NaN argument returns NaN.

Consider the following example for a double argument.

import java.lang.Math;

public class Example4 {
  public static void main(String[] args) {
    double a = 2210.1;
    double b = -3614.450;
    double c = -0.0;

    System.out.println(Math.abs(a));
    System.out.println(Math.abs(b));
    System.out.println(Math.abs(c));
    System.out.println(Math.abs(162.4 / 0));
    System.out.println(Math.abs(162.4 % 0));
  }
}

Output

-> javac Example4.java
-> java Example4
2210.1
3614.45
0.0
Infinity
NaN

That’s it for this tutorial.

See also

Java Math.copySign

Java Math.nextUp

Java Math.nextDown

Java Math.nextAfter

Java Math.max

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.