Java abs() Method: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-04 04:03:01   6739  Share
Subject Syllabus DetailsSubject Details 2 Program
☰ TContent
☰Fullscreen

Table of Content:

Description

This method is overloaded in such a way that all possible data type is handled. Basically the abs() method returns the absolute value of a float value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. However below are some special cases:

  • If the argument is positive zero or negative zero, the result is positive zero.
  • If the argument is infinite, the result is positive infinity.
  • If the argument is NaN, the result is NaN.

The method gives the absolute value of the argument. The argument can be int, float, long, double, short, byte.

Syntax

Following are all the variants of this method ?

double abs(double d)
float abs(float f)
int abs(int i)
long abs(long lng)

Parameters

Here is the detail of parameters ?

  • Any primitive data type.

Return Value

  • This method Returns the absolute value of the argument.

Example

public class AbsMethod {

   public static void main(String args[]) {
      Integer a = -9;
      double d = -120;
      float f = -80;

      System.out.println(Math.abs(a));
      System.out.println(Math.abs(d));
      System.out.println(Math.abs(f));
   }
}

Output

9
120.0
80.0
Press any key to continue . . .

Find absolute value of float, int, double and long using Math.abs

/*
  Find absolute value of float, int, double and long using Math.abs
  This java example shows how to find absolute value of any double,
  float, long or java int value using abs method of Math class.
*/

public class FindAbsoluteValue {

  public static void main(String[] args) {

    int i = 8;
    int j = -5;

    /*
     * To find absolute value of int, use
     * static int abs(int i) method.
     *
     * It returns the same value if the agrument is non negative value, otherwise
     * negation of the negative value.
     *
     */

     System.out.println("Absolute value of " + i + " is :" + Math.abs(i));
     System.out.println("Absolute value of " + j + " is :" + Math.abs(j));

     float f1 = 10.40f;
     float f2 = -50.28f;

    /*
     * To find absolute value of float, use
     * static float abs(float f) method.
     *
     * It returns the same value if the agrument is non negative value, otherwise
     * negation of the negative value.
     *
     */
    System.out.println("Absolute value of " + f1 + " is :" + Math.abs(f1));
    System.out.println("Absolute value of " + f2 + " is :" + Math.abs(f2));

    double d1 = 43.324;
    double d2 = -349.324;
    /*
     * To find absolute value of double, use
     * static double abs(double d) method.
     *
     * It returns the same value if the agrument is non negative value, otherwise
     * negation of the negative value.
     *
     */
    System.out.println("Absolute value of " + d1 + " is :" + Math.abs(d1));
    System.out.println("Absolute value of " + d2 + " is :" + Math.abs(d2));

    long l1 = 34;
    long l2 = -439;
    /*
     * To find absolute value of long, use
     * static long abs(long l) method.
     *
     * It returns the same value if the agrument is non negative value, otherwise
     * negation of the negative value.
     *
     */
    System.out.println("Absolute value of " + l1 + " is :" + Math.abs(l1));
    System.out.println("Absolute value of " + l2 + " is :" + Math.abs(l2));

  }
}

Output

Absolute value of 8 is :8
Absolute value of -5 is :5
Absolute value of 10.4 is :10.4
Absolute value of -50.28 is :50.28
Absolute value of 43.324 is :43.324
Absolute value of -349.324 is :349.324
Absolute value of 34 is :34
Absolute value of -439 is :439
Press any key to continue . . .

Java Math abs() Example

Below is a java code demonstrates the use of abs() method of Math class. The example presented might be simple however it shows the behavior of the abs() method.

import static java.lang.System.out;

/*
 * A java example source code to demonstrate
 * the use of abs() method of Math class
 */

public class MathAbsExample {

	public static void main(String[] args) {

		// initialize values
		int intVal = -123;
		float floatVal = 1.54f;
		double doubleVal = -3.27;

		// get the absoluteValue
		int intValAbs = Math.abs(intVal);
		float floatValAbs = Math.abs(floatVal);
		double doubleValAbs = Math.abs(doubleVal);

		out.println("Absolute value of int value is:"+intValAbs);
		out.println("Absolute value of float value is:"+floatValAbs);
		out.println("Absolute value of double value is:"+doubleValAbs);

	}
}

Output

Absolute value of int value is:123
Absolute value of float value is:1.54
Absolute value of double value is:3.27
Press any key to continue . . .

The above java example source code demonstrates the use of abs() method of Math class. We simply declare values of different values and we get the absolute value of those. This is to demonstrate on how the abs() method works on different data types.