# Java abs Method

The Java abs method is one of the Java Math Library functions, which is to return the absolute (positive) value of the specified expression or an individual number.

## Java abs syntax

The basic syntax of the Math.abs in Java Programming language is as shown below:

`Math.abs(data_type number); //Return Type is Integer`

Number: It can be a number or a valid numerical expression for which you want to find absolute value.

• If the number argument is positive or negative zero, Java Math abs function will return positive zero.
• If the number argument is not a number, Java absolute value will return NaN.

Java Programming provides four different abs functions to find the absolute (positive) value of the specified expression. The following java abs function will accept positive or negative integer value as an argument and returns the absolute value of the integer type.

```static int abs(integer number); //Return Type is Integer
// In order to use in program:
Math.abs(integer number);```

The below Java absolute function will accept positive or negative double value as an argument and returns the absolute value of double type.

```static double abs(double number); //Return Type is double
// In order to use in program:
Math.abs(double number);```

The following math abs function will accept positive or negative float value and returns the absolute value of float type.

```static float abs(float number); //Return Type is float
// In order to use in program:
Math.abs(float number);```

The following Java absolute value function accepts a positive or negative long value. It returns the absolute value of Long type.

```static long abs(integer number); //Return Type is long
// In order to use in program:
Math.abs(long number);```

## Java abs example

We use Java abs Method to find the Java absolute values of different data types and display the output.

```// Java ABS Function
package MathFunctions;

public class AbsMethod {
public static void main(String[] args) {
double b = 10.9666, c = -14.9865;
float d = 1.23f, e = 4.809f;
long f = 200, g = -400;

System.out.println("Absolute value of Positive Integer: " + Math.abs(10));
System.out.println("Absolute value of Negative Integer: " + Math.abs(-20));

System.out.println("\nAbsolute value of Positive Double: " + Math.abs(b));
System.out.println("Absolute value of Negative Double: " + Math.abs(c));

System.out.println("\nAbsolute value of Positive Float: " + Math.abs(d));
System.out.println("Absolute value of Negative Float: " + Math.abs(e));

System.out.println("\nAbsolute value of Positive Number: " + Math.abs(f));
System.out.println("Absolute value of Negative Number: " + Math.abs(g));
}
}```

First, We used the Math.abs Function directly on both the Positive integer and negative integer. The following statements will call the abs method of integer type ( static int abs(integer x) ) and find the corresponding absolute values.

```System.out.println("Absolute value of Positive Integer: " + Math.abs(10));
System.out.println("Absolute value of Negative Integer: " + Math.abs(-20));```

Here, we used the Java absolute value Function on variable b and c (they belong to double type). The following statements will call the abs method of double type ( static double abs(double x) ) and finds corresponding absolute values.

```System.out.println("\nAbsolute value of Positive Double: " + Math.abs(b));
System.out.println("Absolute value of Negative Double: " + Math.abs(c));```

Next, We used the Java Math abs Function on variable d and e (they belong to float type). The following statements will call the abs method of float type ( static float abs(float x) ) and find the corresponding absolute values.

```System.out.println("\nAbsolute value of Positive Float: " + Math.abs(d));
System.out.println("Absolute value of Negative Float: " + Math.abs(e));```

Next, We used the abs Math Function on variable f and g (they belong to long type). The below statements will call the math.abs function of long type ( static long abs(long x) ) to find the corresponding absolute values.

```System.out.println("\nAbsolute value of Positive Number: " + Math.abs(f));
System.out.println("Absolute value of Negative Number: " + Math.abs(g));```

## Java abs on Array example

In this Java program, we find the absolute values of bulk data. Here, we are going to declare an array of integer types and find the absolute values of array elements.

```// Java absolute value example
package MathFunctions;

public class AbsMethodonArray {
public static void main(String[] args) {

int [] myArray = {-15, 45, -20, -15, -6};

for (int i = 0; i < myArray.length; i++) {
System.out.format("Absolute value of Array Element = %d\n", Math.abs(myArray[i]));
}
}
}```
``````Absolute value of Array Element = 15
Absolute value of Array Element = 45
Absolute value of Array Element = 20
Absolute value of Array Element = 15
Absolute value of Array Element = 6``````

In this Java abs array example, First, We declared an Array of integer type and assigned some random values.

`int [] myArray = {-15, 45, -20, -15, -6};`

Next, We used the Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, compiler will check for the condition (i < myArray.length). As along the condition is True statement inside the for loop will be executed.

TIP: myArray.length finds the length of an array.

`for (int i = 0; i < myArray.length; i++) {`

Here, we used the Math.abs Function directly inside the System.out.format statement. Here, the compiler will call the Java abs method of integer type ( static int abs(integer x) ) to find the corresponding absolute values and prints the output.

`System.out.format("Absolute value of Array Element = %d\n", Math.abs(myArray[i]));`

NOTE: To find the absolute value of a single items then use: Math.abs(myArray[index_position])

## Java abs method on Arraylist example

In this Java program, we are going to declare an ArrayList of integer type and find the absolute values of the list elements.

```// Java absolute value example
package MathFunctions;

import java.util.ArrayList;

public class AbsMethodOnList {
public static void main(String[] args) {

ArrayList<Integer> myList = new ArrayList<Integer>(5);

for (Integer x : myList) {
System.out.format("Absolute value of ArrayList = %d\n", Math.abs(x));
}
}
}```
``````Absolute value of ArrayList = 14
Absolute value of ArrayList = 10
Absolute value of ArrayList = 15
Absolute value of ArrayList = 44``````

In this Java abs array list program, we used the For Loop to iterate the ArrayList

`for (Integer x : myList) {`

The following statements will print the output. If you observe the code snippet, we used the Java Math.abs Function directly inside the System.out.format statement.

Here, the compiler will call the abs method of integer type ( static int abs(integer x) ) to find the corresponding absolute values.

`System.out.format("Absolute value of ArrayList = %d\n", Math.abs(x));`