In java Math is a class, which is available in java.lang package. Math class contains many predefined methods we can use those methods to perform required mathematical operations.
The Math.max(x,y) method can be used to find the highest value of x and y:
public class MathsExamples {
public static void main(String[] args) {
int x = 5;
int y = 10;
System.out.println(Math.max(x, y));
}
}
The Math.min(x,y) method can be used to find the lowest value of x and y:
public class MathsExamples {
public static void main(String[] args) {
int x = 5;
int y = 10;
System.out.println(Math.min(x, y));
}
}
The Math.sqrt(x,y) method returns the correctly rounded positive square root value, return data type is double
public class MathsExamples {
public static void main(String[] args) {
int x = 5;
System.out.println(Math.sqrt(x));
}
}
The Math.abs(x) method Returns the absolute value of a double value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned
public class MathsExamples {
public static void main(String[] args) {
int x = -4.8;
System.out.println(Math.max(x));
}
}
The Math.ceil(x) is a Java Math utility method used to round a number UP to the nearest integer value. It accept double as an input and output. For example if we pass 10.1890 it will round-up to the 11.0, if we pass the -10.1890 then it will round-up to the -10.0
public class MathsExamples {
public static void main(String[] args) {
int x = -10.1890;
System.out.println(Math.ceil(x));
}
}