java.lang.StrictMath abs​(float a)

Description

The abs​(float a) method of StrictMath class returns the absolute value of a float value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. Special cases:

  • 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.

Notes:

As implied by the above, one valid implementation of this method is given by the expression below which computes a float with the same exponent and significand as the argument but with a guaranteed zero sign bit indicating a positive value:

Float.intBitsToFloat(0x7fffffff & Float.floatToRawIntBits(a))

The abs​(float a) method of StrictMath class is static thus it should be accessed statically which means the we would be calling this method in this format:

StrictMath.abs​(float a)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java compare method non statically.

Method Syntax

public static double abs​(float a)

Method Argument

Data Type Parameter Description
float a the argument whose absolute value is to be determined.

Method Returns

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

Compatibility

Requires Java 1.3 and up

Java StrictMath abs​(float a) Example

Below is a java code demonstrates the use of abs​(float a) method of StrictMath class. Basically it takes a long input from user and then it displays the absolute value of the long input.

Sample Output

Below is the sample output when you run the above example.

Java StrictMath abs​(float a) method example output