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:
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:
The output is:
Note the expression like Math.abs(-5.2) as argument to the print() method.
abs(float a)
The full syntax for this method is:
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:
The output is:
Note the expression like Math.abs(-5.2f), as argument to the print() method.
abs(int a)
The full syntax for this method is:
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:
The output is:
Note the expression like Math.abs(-5), as an argument to the print() method.
abs(long a)
The full syntax for this method is:
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:
The output is:
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:
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:
The output should be:
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:
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:
The output should be:
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
0 Comments