java.lang.StrictMath abs(int a)
Description
The abs(int a) method of StrictMath class Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.
Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.
Notes:
The abs(int 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(int 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 int abs(int a)
Method Argument
Data Type | Parameter | Description |
---|---|---|
int | a | the argument whose absolute value is to be determined. |
Method Returns
The abs(int a) method returns the absolute value of the argument.
Compatibility
Requires Java 1.3 and up
Java StrictMath abs(int a) Example
Below is a java code demonstrates the use of abs(int a) method of StrictMath class.
package com.javatutorialhq.java.examples; import java.util.Scanner; /* * A java example source code to demonstrate * the use of abs(int a) method of Math class */ public class StrictMathAbsIntExample { public static void main(String[] args) { // Ask user input (int datatype) System.out.print("Enter an int:"); // declare the scanner object Scanner scan = new Scanner(System.in); // use scanner to get the user input and store it to a variable int intValue = scan.nextInt(); // close the scanner object scan.close(); // get the absolute value of the user input int result = StrictMath.abs(intValue); // print the result System.out.println("result: "+result); } }