Java.lang Package Classes

Java Math - abs() Method



The java.lang.Math.abs() method 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. In special cases it returns the following:

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

Syntax

public static int abs(int a)

Parameters

a Specify a number whose absolute value need to be determined.

Return Value

Returns the absolute value (positive value) of the argument.

Exception

NA.

Example:

In the example below, abs() method returns the absolute value of the given int values.

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  int x = 10;
  int y = -10;

  System.out.println(Math.abs(x)); 
  System.out.println(Math.abs(y)); 
 }
}

The output of the above code will be:

10
10

❮ Java.lang - Math