In the earlier tutorials, we learnt about various String methods in Java. In this tutorial let us learn about Java Math class.
Java Math class allows you to perform a number of commonly used mathematical operations in Java. Let us explore some of the commonly used methods in Java Math class.
To find the maximum value out of 2 given numbers, you can use Math.max() method as shown below:
System.out.println(Math.max(2, 4)); // 4
System.out.println(Math.max(25, 5)); // 25
In the first example given above, the program prints 4, which is the maximum value out of 2 and 4.
To find the minimum value out of 2 given number, you can make use of Math.min() method as shown below:
System.out.println(Math.min(2, 4)); // 2
System.out.println(Math.min(25, 5)); // 5
In the first example given above, the program prints 2, which is the minimum value out of 2 and 4.
If you have numbers which can return both positive or negative values, then you can make use of Math.abs() method to get the absolute value (i.e. the positive value) of the number as shown below:
System.out.println(Math.abs(-11.5)); //11.5
To find the square root of a number, you can use the Math.sqrt() method as shown below:
System.out.println(Math.sqrt(25)); // 5.0
System.out.println(Math.sqrt(121)); // 11.0
This method returns a value of double datatype. Hence, in case of non-perfect square numbers, it will return numbers with decimal values as shown below:
System.out.println(Math.sqrt(26)); // 5.0990195135927845
If you want to find the exponent of a number, you can use the Math.pow() method as shown below:
System.out.println(Math.pow(3,4)); // 81.0
If you have a number with decimals and would like to round it off to its nearest integer. Then you can make use of Math.round() method as shown below:
System.out.println(Math.round(3.4)); // 3
System.out.println(Math.round(3.8)); // 4
System.out.println(Math.round(3.5)); // 4
This method will be return the nearest integer higher than the number in case the decimal is equal to or more than 0.5. In case of decimal value below 0.5, the method will return the nearest integer lower than the number.
If you have a number with decimals and would like to round it off to its nearest integer greater than the number, then you can make use of Math.ceil() method as shown below:
System.out.println(Math.ceil(4.4)); // 5.0
System.out.println(Math.ceil(4.8)); // 5.0
System.out.println(Math.ceil(4.5)); // 5.0
If you have a number with decimals and would like to round it off the its nearest integer which is lesser than the number, then you can make use of Math.floor() method as shown below:
System.out.println(Math.floor(2.4)); // 2.0
System.out.println(Math.floor(2.8)); // 2.0
System.out.println(Math.floor(2.5)); // 2.0
If you want to generate a random number, you can make use of Math.random() method as shown below.
System.out.println(Math.random());
This method will return a number greater than or equal to 0 to a number less than 1. E.g. 0.598666. As the name suggest, it will return a different number every time you execute this code.
I hope this chapter was helpful. See you in the next chapter of Java tutorial series.
Fully customizable CRM Software for Freelancers and Small Businesses