Java Math.abs - Syntax
mixed abs(mixed n);
The Math.abs() function in Java is used to return the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign.
mixed abs(mixed n);
mixed n |
Specifies the real number (int, long, float, double) / infinity (*Required) |
---|---|
Return mixed |
Returns a number / infinity respective to an input argument. |
Convert the positive and negative integer to an absolute value.
import java.lang.Math;
class MathExample {
public static void main(String[] args) {
// Positive integer value
int n = Math.abs(9);
System.out.println("The absolute value of 9 is " + n);
// Negative integer value
int x = Math.abs(-6);
System.out.println("The absolute value of -6 is " + x);
}
}
Convert the positive and negative float to an absolute value.
import java.lang.Math;
class MathExample {
public static void main(String[] args) {
// Positive floating point value
double n = Math.abs(9.3);
System.out.println("The absolute value of 9.3 is " + n);
// Negative floating point value
double x = Math.abs(-6.3);
System.out.println("The absolute value of -6.3 is " + x);
}
}
Convert the positive and negative Infinity to an absolute value.
import java.lang.Math;
class MathExample {
public static void main(String[] args) {
// Positive ∞ infinite value
double n = Math.abs(Double.POSITIVE_INFINITY);
System.out.println("The absolute value of Infinity is " + n);
// Negative ∞ infinite value
double x = Math.abs(-Double.POSITIVE_INFINITY);
System.out.println("The absolute value of -Infinity is " + x);
}
}