math

Java Absolute Value Example

In this post, we feature a comprehensive Java Absolute Value Example. With this example we are going to demonstrate how to calculate the absolute value of a number, using the Math.abs method of Java Math Class. The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, trigonometric functions and most importantly Math.abs java function. In short, to calculate the absolute value of a number, using Math you should:

  • Use abs(double a), abs(float a), abs(int a), abs(long a) API methods of Math to get the absolute value of a double, a float, an int or a long number.
  • Alternatively, create your own abs() functions and use them instead of those belonging to Math class.
Java Absolute Value- graph
abs Graph

Let’s take a look at the code snippet that follows.

Here we use the Math.abs method to get the absolute value on the one case, on the other case we implement one method for each type (int, double, float, long) and use them the same way.

package com.javacodegeeks.snippets.core;

public class CalculateAbsoluteValue {

    public static void main(String[] args) {

        int i = 5;
        int j = -3;

        // absolute value of integer using Math.abs
        System.out.println("Absolute value of " + i + " is: " + Math.abs(i));
        System.out.println("Absolute value of " + j + " is: " + Math.abs(j));
        // absolute value of integer using custom abs method
        System.out.println("Absolute value of " + i + " is: " + abs(i));
        System.out.println("Absolute value of " + j + " is: " + abs(j));

        float f1 = 44.21f;
        float f2 = -25.89f;

        // absolute value of float using Math.abs
        System.out.println("Absolute value of " + f1 + " is: " + Math.abs(f1));
        System.out.println("Absolute value of " + f2 + " is: " + Math.abs(f2));
        // absolute value of float using custom abs method
        System.out.println("Absolute value of " + f1 + " is: " + abs(f1));
        System.out.println("Absolute value of " + f2 + " is: " + abs(f2));

        double d1 = 15.246;
        double d2 = -985.33;

        // absolute value of double using Math.abs
        System.out.println("Absolute value of " + d1 + " is: " + Math.abs(d1));
        System.out.println("Absolute value of " + d2 + " is: " + Math.abs(d2));
        // absolute value of double using custom abs method
        System.out.println("Absolute value of " + d1 + " is: " +abs(d1));
        System.out.println("Absolute value of " + d2 + " is: " +abs(d2));

        long l1 = 3344L;
        long l2 = -13349L;

        // absolute value of long using Math.abs
        System.out.println("Absolute value of " + l1 + " is: " + Math.abs(l1));
        System.out.println("Absolute value of " + l2 + " is: " + Math.abs(l2));
        // absolute value of long using custom abs method
        System.out.println("Absolute value of " + l1 + " is: " + abs(l1));
        System.out.println("Absolute value of " + l2 + " is: " + abs(l2));


    }

    private static int abs(int x){
        if(x>=0){
            return x;
        }
        else {
            return -x;
        }
    }

    private static float abs(float x){
        if(x>=0){
            return x;
        }
        else {
            return -x;
        }
    }

    private static double abs(double x){
        if(x>=0){
            return x;
        }
        else {
            return -x;
        }
    }

    private static long abs(long x){
        if(x>=0){
            return x;
        }
        else {
            return -x;
        }
    }

}

Output:

Absolute value of 5 is: 5
Absolute value of -3 is: 3
Absolute value of 5 is: 5
Absolute value of -3 is: 3
Absolute value of 44.21 is: 44.21
Absolute value of -25.89 is: 25.89
Absolute value of 44.21 is: 44.21
Absolute value of -25.89 is: 25.89
Absolute value of 15.246 is: 15.246
Absolute value of -985.33 is: 985.33
Absolute value of 15.246 is: 15.246
Absolute value of -985.33 is: 985.33
Absolute value of 3344 is: 3344
Absolute value of -13349 is: 13349
Absolute value of 3344 is: 3344
Absolute value of -13349 is: 13349

As you can see, the results are exactly what we expected.

This was an example of how to calculate the absolute value of a number, using the abs() method of Math Class in Java.

Java Absolute Value Example – Download

Download
You can download the full source code of this example here: Absolute Value Example

Last updated on Aug. 2nd, 2019

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button