abs() in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The Math.abs() method is the inbuilt method of the Math class that is present in java.lang package. The Math.abs() method takes one parameter that is of number type and returns its absolute value, i.e. the positive value of the number, without using the negative sign. For example, the absolute value of -4 is 4.

Syntax of abs() in Java

The syntax for abs() method in Java is given below. The Math.abs() takes only one parameter of type number or floating-point: ' int, float, long, and double`.

Syntax:

Parameters of abs() in Java

The Math.abs() takes only one parameter that is of int, float, long, double datatype. The Math.abs() method is valid for these datatypes only.

Valid syntax of the abs() method in java:

These are the valid syntax of the abs() method with the valid parameter.

Return values of abs() in Java

The Math.abs() returns an absolute value of the number that is a non-negative representation of the number. The return value type depends upon the parameter; if the parameter's datatype is int or float, the return type will be int and float, respectively.

If the non-negative number is passed as a parameter, the abs() method will return the same number itself.

Exceptions of abs() in Java

The Math.abs() method generally throws an exception, a mismatch exception, when the type of parameter is different from the number.

Let's understand the exception using an example:

Output:

The "Scaler" is the type of String; that's why abs() method causes an exception because it cannot handle the String datatypes.

Example of abs() in Java

Let's understand the abs() method using an example.

Output:

The absolute value of -4 is 4, which is why 4 is printed.

More about abs() in Java

  • In many cases, the abs() method fails to find the absolute value of the given value. The cases are given below: the absolute value of Integer.MIN_VALUE, and in the case of Integer.MAX_VALUE, the abs() method returns Integer.MAX_VALUE without any change.
  • The abs() method is used to avoid negative calculation errors. For example, during finding the roots of the quadratic equation, i.e. sqrt(b24ac)sqrt(b^2-4*a*c), we can pass the body of the expression as a parameter of the abs() method. Because the root of the negative element doesn't exist. The abs() method converts the negative value to the positive.
  • The abs() method works only for number datatypes.

Practical Example of abs() in Java

Using an example, let's understand the importance of using the abs() method.

We have an equation that is sqrt(b24ac)sqrt(b^2-4*a*c), and we have the values of three variables: ' a, bandc`. The expression, i.e. b24acb^2-4*a*c, can be less than zero.

Now, the question is, what will happen if we try to find the sqrt of the negative value?

The answer is we get NAN, which is Not a number. To avoid this, we have to use the abs() method, which will help convert the negative number to the positive. In this way, we can evaluate the expression easily.

Let's understand this using an example:

Output:

We cannot find the square root of a negative element because a Nan will be returned if we try. So, we have to use the abs() method to avoid the Nan.

Example 1: The Absolute Value of Integer.MIN_VALUE

Let's try to find the absolute value of Integer.MIN_VALUE

Output:

Why do we get a negative value instead of a positive one in the first case?

The answer is the absolute value of the Integer. ' MIN_Value, i.e., 2147483648` is greater than the range of int, so a negative value is returned.

In the second case, we typecast the int minimum value to the long type, and we know that the range of long for storing the numbers is greater than that of int; that's why the correct absolute value is returned.

Example 2: Find the Absolute Value of -0

We know 0 is neither a positive number nor a negative number. Let's check what is the absolute value of -0:

Output:

The absolute value of -0 is 0.

Example 3: Convert All the Negative Elements of an Array to Positive.

Given an array of n elements, convert all the negative elements of an array to positive. Let's understand this using an example:

In the example below, we iterate on the elements of the array and convert the negative elements to positive using the abs() method.

Output:

We use abs() method to convert all the negative values to positive ones.

Conclusion

  • The Math.abs() method is present in the java Math class.lang package.
  • The Math.abs() method is used to find the absolute value of numerical data types.
  • While calculating the roots of the quadratic equation, we use the Math.abs() method to avoid the root of negative numbers.