Java abs Method

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

Java abs method 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: A number or a valid numerical expression for which you want to find absolute value.

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

Java Programming provides four abs functions to find the specified expression’s absolute (positive) value. The following 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 Java 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 function accepts a positive or negative long value. It returns the absolute value of the 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 absolute values of different data types and display the output.

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));
	}
}
Java ABS Method to find absolute value 1

First, We used the Java Math class abs Function directly on both positive and negative integers. 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 variables b and c (they belong to double type). The following statements will call the static double abs(double x) of double type and find 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 variables 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 variables f and g (they belong to the long type). The below statements will call the static long abs(long x) ) function of long type 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 will declare an array of integer types and find the absolute values of array elements.

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

Analysis

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

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

Next, We used Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < myArray.length). As 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++) {

We used this 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 positive numbers 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 item, then use: Math.abs(myArray[index_position])

Java abs method on Arraylist example

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

package MathFunctions;

import java.util.ArrayList;

public class AbsMethodOnList {
	public static void main(String[] args) {
		
		ArrayList<Integer> myList = new ArrayList<Integer>(5);
		myList.add(-14);
		myList.add(-10);
		myList.add(15);
		myList.add(-44);
		
		for (Integer x : myList) {
			System.out.format("Positive value of ArrayList = %d\n", Math.abs(x));
		}
	}
}
Positive value of ArrayList = 14
Positive value of ArrayList = 10
Positive value of ArrayList = 15
Positive value of ArrayList = 44

Analysis

We used the For Loop to iterate the ArrayList in this abs array list program.

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 static int abs(integer x) of integer type to find the corresponding positive.

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