Java Math abs() Method

tutorial and example
tutorial and example

The abs() method of Math class returns the absolute value of the argument where the argument can be int, double, float, long.

Syntax

public static int abs(int a)

public static float abs(float a)

public static double abs(double a)

public static long(long a)

Parameters

The argument whose absolute value is to be determined

Return Value

The abs() method returns the absolute value of the argument.

  • If the argument passed is positive, it will return positive value.
  • If the argument passed is Infinity, it will return positive infinity.
  • If the argument passed is NaN, it will return NaN.

Example 1:

Example 2:

Output:

Example 3:

Output:

Example 4:

Output: