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

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

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 is used to find the length of an array.

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

Following statements will print the output. you observe the code snippet, we used the Math.abs Function directly inside the System.out.format statement. Here, compiler will call the Java abs method of integer type ( static int abs(integer x) ) to find the corresponding absolute values.