Java Math
The Java Math class has many methods which helps to perform mathematical tasks on numbers.
Math.max(x,y) – This method can be used to find the highest value of x and y.
Math.min(x,y) – This method can be used to find the lowest value of x and y.
Math.sqrt(x) – This method will return the square root of the value x.
Math.abs(x) – This method the absolute (positive) value of x.
Math.random() – This method returns number between 0 (inclusive) and 1 (exclusive).
Example:
int x = 10; int y = 20; System.out.println("Maximum of x and y : " +Math.max(x, y)); System.out.println("Minimum of x and y : " +Math.min(x, y)); System.out.println("Square of x : " +Math.sqrt(x)); System.out.println("Absolue value of -10.7 : " +Math.abs(-10.7)); System.out.println("Random value : " +Math.random());
Output:
Maximum of x and y : 20 Minimum of x and y : 10 Square of x : 3.1622776601683795 Absolue value of -10.7 : 10.7 Random value : 0.46976251485316733