Java Math abs() method with Examples
The java.lang.Math.abs() returns the absolute value of a given argument.
- If the argument is not negative, the argument is returned.
- If the argument is negative, the negation of the argument is returned.
Syntax :
public static DataType abs(DataType a) a : the argument whose absolute value is to be determined Returns : This method returns the absolute value of the argument.
- If the argument is of double or float type:
- If the argument is positive zero or negative zero, the result is positive zero.
- If the argument is infinite, the result is positive infinity.
- If the argument is NaN, the result is NaN.
- If the argument is of int or long type:
- If the argument is equal to the value of Integer.MIN_VALUE or Long.MIN_VALUE, the most negative
representable int or long value, the result is that same value, which is negative.
- If the argument is equal to the value of Integer.MIN_VALUE or Long.MIN_VALUE, the most negative
// Java program to demonstrate working // of java.lang.Math.abs() method import java.lang.Math; class Gfg { // driver code public static void main(String args[]) { float a = 123.0f; float b = -34.2323f; double c = -0.0; double d = -999.3456; int e = -123; int f = -0; long g = -12345678; long h = 98765433; // abs() method taking float type as input System.out.println(Math.abs(a)); System.out.println(Math.abs(b)); System.out.println(Math.abs(1.0 / 0)); // abs() method taking double type as input System.out.println(Math.abs(c)); System.out.println(Math.abs(d)); // abs() method taking int type as input System.out.println(Math.abs(e)); System.out.println(Math.abs(f)); System.out.println(Math.abs(Integer.MIN_VALUE)); // abs() method taking long type as input System.out.println(Math.abs(g)); System.out.println(Math.abs(h)); System.out.println(Math.abs(Long.MIN_VALUE)); } } |
chevron_right
filter_none
Output:
123.0 34.2323 Infinity 0.0 999.3456 123 0 -2147483648 12345678 98765433 -9223372036854775808
Recommended Posts:
- Java Math max() method with Examples
- Java Math tan() method with Examples
- Math fma() method in Java with Examples
- Java Math ulp() method with Examples
- Java Math min() method with Examples
- Java Math sin() method with Examples
- Java Math cos() method with Examples
- Math multiplyFull() method in Java with Examples
- Java Math sinh() method with Examples
- Java Math cosh() method with Examples
- Java Math acos() method with Examples
- Java Math tanh() method with Examples
- Java Math random() method with Examples
- Java Math nextUp() method with Examples
- Java Math IEEEremainder() method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



