Example of Math.abs() in Java

UPDATED: 31 December 2015
java.lang.Math

Math.abs(int a)
Returns the absolute value of an int value.
 - If the argument is not negative, the argument is returned.
 - If the argument is negative, the negation of the argument is returned.
Also Math.abs(long a), Math.abs(float a) and Math.abs(double a) is available in java.lang.Math class.

Source code (MathAbsExample.java)
/**
 * Example: Math.abs
 * @author javaQuery
 * @date 31st December, 2015
 * @Github: https://github.com/javaquery/Examples
 */
public class MathAbsExample {

    public static void main(String[] args) {
        int x = 1, y = -2;

        System.out.println("Before:");
        System.out.println("x = " + x + ", y = " + y);

        /**
         * Math.abs(int a)
         * Returns the absolute value of an int value. 
         *  - If the argument is not negative, the argument is returned. 
         *  - If the argument is negative, the negation of the argument is returned.
         * 
         * Math.abs(int a) => return 'int'
         * Math.abs(long a) => return 'long'
         * Math.abs(float a) => return 'float'
         * Math.abs(double a) => return 'double'
         */
        x = Math.abs(x);
        y = Math.abs(y);
        
        System.out.println("After:");
        System.out.println("x = " + x + ", y = " + y);
    }
}

Output
Before:
x = 1, y = -2
After:
x = 1, y = 2

0 comments :