Absolute Value in Java

A number can be an int, a long, a float, or a double. The difference between an int and a long variable is that the long variable can hold a bigger value than an int variable. A long variable can also hold a number that an int would hold. Each of these number types can be negative or positive. For example, an int can be -5 or +5. When it is +5, the plus sign in front of it can be omitted.

So, a number can be negative or positive. Absolute value is simply the positive value of the pair. The positive value is still the number without a sign. Java has the math abs() method to return the absolute number. So, if the argument is -5, 5 will be returned. If the argument is +5 or 5, 5 will be returned. Java also has the absExact() method – see below.

These methods are all of the Math class. The Math class does not have to be imported by the programmer to be used. This article explains the use of the abs() and absExact() methods of the math class.

abs(double a)

The full syntax for this method is:

public static double abs(double a)

It is public, meaning that it can be called from outside the class implementation. It is static, meaning that the Math class does not have to be instantiated before it is used (with the method). To call the method, begin with the class name, Math, followed by the dot operator, and then the method with its argument. Its argument should be double in order to return a double. The following program illustrates its use:

    public class TheClass {
        public static void main(String[] args) {
System.out.print(Math.abs(-5.2)); System.out.print(", ");
System.out.print(Math.abs(+5.2)); System.out.print(", ");
System.out.print(Math.abs(5.2)); System.out.print(' ');
System.out.println(' ');
        }
    }

The output is:

5.2, 5.2, 5.2

Note the expression like Math.abs(-5.2) as argument to the print() method.

abs(float a)

The full syntax for this method is:

public static float abs(float a)

It is public, meaning that it can be called from outside the class implementation. It is static, meaning that the Math class does not have to be instantiated before it is used (with the method). To call the method, begin with the class name, Math, followed by the dot operator, and then the method with its argument. Its argument should be a float in order to return a float. The following program illustrates its use:

    public class TheClass {
        public static void main(String[] args) {
System.out.print(Math.abs(-5.2f)); System.out.print(", ");
System.out.print(Math.abs(+5.2f)); System.out.print(", ");
System.out.print(Math.abs(5.2f)); System.out.print(' ');
System.out.println(' ');
        }
    }

The output is:

5.2, 5.2, 5.2

Note the expression like Math.abs(-5.2f), as argument to the print() method.

abs(int a)

The full syntax for this method is:

public static int abs(int a)

It is public, meaning that it can be called from outside the class implementation. It is static, meaning that the Math class does not have to be instantiated before it is used (with the method). To call the method, begin with the class name, Math, followed by the dot operator, and then the method with its argument. Its argument should be an int in order to return an int. The following program illustrates its use:

    public class TheClass {
        public static void main(String[] args) {
System.out.print(Math.abs(-5)); System.out.print(", ");
System.out.print(Math.abs(+5)); System.out.print(", ");
System.out.print(Math.abs(5)); System.out.print(' ');
System.out.println(' ');
        }
    }

The output is:

5, 5, 5

Note the expression like Math.abs(-5), as an argument to the print() method.

abs(long a)

The full syntax for this method is:

public static long abs(long a)

It is public, meaning that it can be called from outside the class implementation. It is static, meaning that the Math class does not have to be instantiated before it is used (with the method). To call the method, begin with the class name, Math, followed by the dot operator, and then the method with its argument. Its argument should be long, in order to return a long. The following program illustrates its use:

    public class TheClass {
        public static void main(String[] args) {
System.out.print(Math.abs(-5)); System.out.print(", ");
System.out.print(Math.abs(+5)); System.out.print(", ");
System.out.print(Math.abs(5)); System.out.print(' ');
System.out.println(' ');
        }
    }

The output is:

5, 5, 5

Note the expression like Math.abs(-5), as an argument to the print() method.

absExact(int a)

This method should be used when the user insists on an int argument. The full syntax for this method is:

public static int absExact(int a)

It is public, meaning that it can be called from outside the class implementation. It is static, meaning that the Math class does not have to be instantiated before it is used (with the method). To call the method, begin with the class name, Math, followed by the dot operator, and then the method with its argument. Its argument should be an int in order to return an int. The following program illustrates its use:

    public class TheClass {
        public static void main(String[] args) {
System.out.print(Math.absExact(-5)); System.out.print(", ");
System.out.print(Math.absExact(+5)); System.out.print(", ");
System.out.print(Math.absExact(5)); System.out.print(' ');
System.out.println(' ');
        }
    }

The output should be:

5, 5, 5

Note the expression like Math.absExact(-5), as argument to the print() method.

absExact(long a)

This method should be used when the user insists on a long argument. The full syntax for this method is:

public static long absExact(long a)

It is public, meaning that it can be called from outside the class implementation. It is static, meaning that the Math class does not have to be instantiated before it is used (with the method). To call the method, begin with the class name, Math, followed by the dot operator, and then the method with its argument. Its argument should be a long, in order to return a long. The following program illustrates its use:

    public class TheClass {
        public static void main(String[] args) {
System.out.print(Math.absExact(-5)); System.out.print(", ");
System.out.print(Math.absExact(+5)); System.out.print(", ");
System.out.print(Math.absExact(5)); System.out.print(' ');
System.out.println(' ');
        }
    }

The output should be:

5, 5, 5

Note the expression like Math.absExact(-5), as argument to the print() method.

Conclusion

The abs() and absExact() methods are of the Java Math class. They are public, as well as static. Each returns the absolute value of the argument number. To insist on using an int or a long argument, use absExact().



from https://ift.tt/33RNM7a

Post a Comment

0 Comments