In Java, the “Math” class is a part of the “java.lang” package that provides various mathematical operations and functions. Among the multiple methods offered by the Math class, the “abs()” method is used to find the absolute value of a numeric expression.
This article will explore the functionality and usage of the Java Math “abs()” method, along with code examples.
What is Absolute Value?
The positive value that corresponds to the number passed as an argument is referred to as the absolute value. It refers to the fact that, regardless of whether a positive or negative number is provided for computation, the computation will always take place over the positive matching number such as the absolute value of -3 is 3.
Therefore, we do have a method in Java called “abs()” in the Math class present within the “java.lang” package in order to compute the absolute value for any number.
What is Java Math abs() Method?
The Java Math “abs()” is a frequently used mathematical method that allows developers to obtain the absolute value of a given number. Absolute value, also referred to as magnitude, is a distance of a number from zero on the number line, regardless of the number’s sign.
This method can be especially useful in scenarios where we want to ensure that a value is always positive or when solving mathematical problems involving distances or differences.
Syntax
public static double abs(double a)
Here, “double” indicates the type of argument, and “a” corresponds to the input number for which the absolute value is requested.
“Java.lang.Math.abs()” returns an argument’s absolute value.
- The argument is provided if it is not unfavorable.
- The negation of the argument is provided back in the scenario of the argument being negative.
Code Examples
Let’s explore some examples to better understand the usage and functionality of the Math “abs()” method.
Example 1: Finding the Absolute Value of Integers
To obtain the absolute value from integers, we can simply invoke the abs() method as follows:
package com.company;
public class Class1 {
public static void main(String[] args) {
int num = -32;
int absolute_num1 = Math.abs(num);
System.out.println("Absolute value of " + num + " is " + absolute_num1);
}
}
Here, we’ll utilize the Math “abs()” method to determine the absolute values of the integer “num“. The method returns the absolute number i.e., 32 against the value “-32“.
Output

Example 2: Computing the Absolute Value of Floating-Point Numbers
The Math abs() method can also be used with floating-point numbers, as follows:
package com.company;
public class Class1 {
public static void main(String[] args) {
double num1 = -23.57;
System.out.println("Absolute value of " + num1 + " is " + Math.abs(num1));
}
}
In the above code, we have a variable “num1” of type double, with an assigned value of -23.57. Applying the “abs()” method to this variable would yield the absolute value “23.57” by negating the minus sign.
Output

Example 3: Computing the Absolute Value of Long Numbers
Additionally, the abs() method can handle long values:
package com.company;
public class Class1 {
public static void main(String[] args) {
long num1 = -1000000000;
System.out.println("Absolute value of " + num1 + " is " + Math.abs(num1));
}
}
In this code, the long variable named “num1” is initialized with -1000000000. To obtain the absolute value in this case, we can use the “Math.abs()” method. The result will then be printed on the screen.
Output

Example 4: Computing the Absolute Value of Expressions
The abs() method is not limited to single variables; it can also be applied to more complex expressions.
Consider the following scenario: we have two integers a and b, and we want to determine the absolute difference between them. We can achieve this by calculating “Math.abs(a – b)”.
Regardless of whether a is greater than or less than b, the “abs()” method will yield a positive value, signifying the magnitude of the difference.
Following is the code demonstration:
package com.company;
public class Class1 {
public static void main(String[] args) {
int res = Math.abs(3 - 14);
System.out.println("Absolute value of the result is " + res);
}
}
In this example, the “Math.abs()” method is applied to compute the absolute value of the expression “3 – 14”. The evaluated result is “-11” and the method returns its absolute value, which is “11”.
Output

Example 5: Calculating the Absolute Value of Edge Cases
It is worth mentioning that the “abs()” method provides consistent behavior for edge cases. For instance, if we pass “Integer.MAX_VALUE” or “Integer.MIN_VALUE” to the abs() method, it will return the same value, as these extreme values already represent the absolute range of the int data type.
The following code clarifies the discussed concept:
package com.company;
public class Class1 {
public static void main(String[] args) {
int abs = Math.abs(Integer.MIN_VALUE);
System.out.println("Absolute value of the result is " + abs);
long longAbs1 = Math.abs((long) Integer.MIN_VALUE);
System.out.println("Absolute value of the result is " + longAbs1);
}
}
According to the above code lines:
- First, find out the absolute value of the Integer.MIN_VALUE. Since “MIN_Value” or “2147483648” exceeds the int range, a negative number is returned.
- Since we typecast the “Integer.MIN_VALUE” to the long type in the second scenario and are fully aware that the long type has a larger storage range than the int, the proper absolute value is returned.
Output

Example 6: Finding the Absolute Value of -0
We are aware that zero is not a positive or a negative number. Let’s calculate the absolute value of “-0”:
package com.company;
public class Class1 {
public static void main(String[] args) {
int zero = Math.abs(-0);
System.out.println("The absolute value of 0 is: " + zero);
}
}
In this code, we find the absolute value of “-0” using the “Math.abs(-0)” and store it in the “zero” variable. Then we print it out on the screen.
Output

Example 7: Computing the Absolute Values of Integers in an Array
This example converts all of the negative integers in an array of n items to positive:
package com.company;
public class Class1 {
public static void main(String[] args) {
int arr1[] = {
-4,
22,
-43
};
for (int x = 0; x < arr1.length; ++x) {
arr1[x] = Math.abs(arr1[x]);
System.out.print(arr1[x] + " ");
}
}
}
In the above example, iterate through the array values via “for” loop and apply the “abs()” method to transform the negative values into positives.
Output

Conclusion
The Java Math “abs()” method is a valuable asset for developers, providing a quick and efficient means of obtaining the absolute value of a given numerical value. It allows developers to handle a wide range of numeric types and simplifies calculations involving magnitudes.