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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.javatutorialhq.java.examples; import java.util.Scanner; /* * A java example source code to demonstrate * the use of abs(float a) method of Math class */ public class StrictMathAbsFloatExample { public static void main(String[] args) { // Ask user input (float datatype) System.out.print("Enter a float:"); // declare the scanner object Scanner scan = new Scanner(System.in); // use scanner to get the user input and store it to a variable float fValue = scan.nextFloat(); // close the scanner object scan.close(); // get the absolute value of the user input float result = StrictMath.abs(fValue); // print the result System.out.println("result: "+result); } } |