Key Features of Math.abs()
- Non-negative Result: It always returns a positive value or zero.
- Multiple Overloads: It works with different data types including int, long, float, and double.
- Simple Usage: It is straightforward to use and understand.
Syntax of Math.abs()
The Math.abs() method can be used with several types of numbers. The syntax varies slightly depending on the data type:
For int:
int absValue = Math.abs(int a);
For double:
double absValue = Math.abs(double a);
How Math.abs() Works
The Math.abs() method takes a single argument and returns its absolute value. Here’s how it works:
- Positive Input: If the input number is positive, it returns the same number.
- Negative Input: If the input number is negative, it returns the positive counterpart.
- Zero: If the input number is zero, it simply returns zero.
Examples of Math.abs() Usage
Let’s see how Math.abs() works with different data types through some examples.
Example 1: Using Math.abs() with int
Java
public class MathAbsExample {
public static void main(String[] args) {
int negativeInt = -10;
int positiveInt = Math.abs(negativeInt);
System.out.println("Absolute value of " + negativeInt + " is " + positiveInt);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Absolute value of -10 is 10
Example 2: Using Math.abs() with double
Java
public class MathAbsExample {
public static void main(String[] args) {
double negativeDouble = -3.14159;
double positiveDouble = Math.abs(negativeDouble);
System.out.println("Absolute value of " + negativeDouble + " is " + positiveDouble);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Absolute value of -3.14159 is 3.14159
Frequently Asked Questions
What happens if I use Math.abs() with Integer.MIN_VALUE?
The result is Integer.MIN_VALUE itself because Integer.MIN_VALUE cannot be represented as a positive number within the integer range.
Can Math.abs() handle NaN values?
No, Math.abs() does not handle NaN (Not a Number). For floating-point numbers, if the input is NaN, the result will also be NaN.
Is Math.abs() useful in sorting algorithms?
Yes, it can be useful for sorting algorithms that require absolute values, such as when sorting distances or magnitudes.
Conclusion
The Math.abs() method in Java is a simple yet powerful tool for obtaining the absolute value of numbers. Whether you are dealing with integers, long values, or floating-point numbers, Math.abs() can handle it all, ensuring that your calculations are always non-negative. By understanding and applying this method, you can effectively manage absolute values in your Java applications.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.