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.
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:
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.
Math.abs(value);
The method returns the absolute value of the specified number.
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
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
The Math.abs() method provides overloaded versions for different primitive numeric data types.
| Data Type | Method | Return Type |
|---|---|---|
| int | Math.abs(int) | int |
| long | Math.abs(long) | long |
| float | Math.abs(float) | float |
| double | Math.abs(double) | double |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India