Java Absolute Value

Last Updated : 30 Sept 2026

The absolute value of a number represents its non-negative value, regardless of whether the original number is positive or negative. Java provides the Math.abs() method to calculate the absolute value of different numeric data types.

In this chapter, you will learn about the absolute value in Java and how to calculate it using the Math.abs() method.

What is Absolute Value in Java?

The absolute value of a number is its distance from zero on the number line. A positive number remains unchanged, while a negative number is converted to its corresponding positive value.

For example:

  • Absolute value of 10 is 10.
  • Absolute value of -10 is 10.
  • Absolute value of 0 is 0.

Math.abs() Method in Java

The Math.abs() method is used to return the absolute value of a numeric value. It is available for int, long, float, and double values.

Syntax

Math.abs(value);

The method returns the absolute value of the specified number.

Example of Absolute Value in Java

Example 1: Finding the Absolute Value of an Integer

In the following example, we use the Math.abs() method to find the absolute value of a negative integer.

Java

public class Main {
    public static void main(String[] args) {
        int number = -25;

        int result = Math.abs(number);

        System.out.println("Absolute value: " + result);
    }
}

Output:

Absolute value: 25

Example 2: Finding the Absolute Value of a Double

In the following example, we use the Math.abs() method to find the absolute value of a negative double value.

Java

public class Main {
    public static void main(String[] args) {
        double number = -15.75;

        double result = Math.abs(number);

        System.out.println("Absolute value: " + result);
    }
}

Output:

Absolute value: 15.75

Math.abs() with Different Data Types

The Math.abs() method provides overloaded versions for different primitive numeric data types.

Data TypeMethodReturn Type
intMath.abs(int)int
longMath.abs(long)long
floatMath.abs(float)float
doubleMath.abs(double)double

Important Points About Absolute Value in Java

  • Math.abs() is defined in the Math class.
  • It can be used with int, long, float, and double values.
  • A positive value remains unchanged.
  • A negative value is converted to its corresponding positive value.
  • The absolute value of 0 is 0.
  • Math.abs() returns a value of the same primitive numeric type as its argument.

Next TopicJava Tutorial