Math class in Java

The java.lang.Math class is a built-in class in Java that provides methods for performing basic numeric operations. This class is located in the java.lang package, so we do not need to import it in order to use it. All of the methods in the Math class are static, so we do not need to create an instance of the Math class to use them.

Operations in Math class in Java

The Math class includes methods for the following operations:

  • Absolute value
  • Maximum and minimum of two values
  • Rounding
  • Square root
  • Trigonometric functions (sine, cosine, and tangent)
  • Random number generation

Find absolute value of a number in Java

Following is an example of how to use the Math class to find the absolute value of a number:

double num = -9.0;
double absNum = Math.abs(num);
System.out.println("Absolute value of " + num + " is " + absNum);

Output:

Absolute value of -9.0 is 9.0

Find max and min values in Java

We can use the max and min methods to find the greater or smaller of two values, respectively. For example:

double num1 = 9.0;
double num2 = 3.0;
double max = Math.max(num1, num2);
double min = Math.min(num1, num2);
System.out.println("Greater number between " + num1 + " and " + num2 + " is " + max);
System.out.println("Smaller number between " + num1 + " and " + num2 + " is " + min);

Output:

Greater number between 9.0 and 3.0 is 9.0
Smaller number between 9.0 and 3.0 is 3.0

Round value in Java

The round method allows us to round a float value to the nearest integer. For example:

float num = 9.6f;
int roundedNum = Math.round(num);
System.out.println("Rounded value of " + num + " is " + roundedNum);

Output:

Rounded value of 9.6 is 10

Find square root of value in Java

The sqrt method returns the square root of a given value. For example:

double num = 9.0;
double sqrtNum = Math.sqrt(num);
System.out.println("Square root of " + num + " is " + sqrtNum);

Output:

Square root of 9.0 is 3.0

Find sin cos and tan values in Java

The sin, cos, and tan methods allow us to calculate the sine, cosine, and tangent of a given value, respectively. These methods expect the argument to be in radians, not degrees. We can use the toRadians method of the Math class to convert from degrees to radians if necessary.

For example:

double num = 90.0;
double radians = Math.toRadians(num);
double sinNum = Math.sin(radians);
double cosNum = Math.cos(radians);
double tanNum = Math.
tan(radians);
System.out.println("Sine of " + num + " degrees is " + sinNum);
System.out.println("Cosine of " + num + " degrees is " + cosNum);
System.out.println("Tangent of " + num + " degrees is " + tanNum);

Output:

Sine of 90.0 degrees is 1.0
Cosine of 90.0 degrees is 6.123233995736766E-17
Tangent of 90.0 degrees is 1.633123935319537E16

Generate random number in Java

Finally, the random method generates a random double value between 0 (inclusive) and 1 (exclusive). We can use this method to generate random numbers for various purposes, such as shuffling an array or simulating dice rolls.

For example:

double random = Math.random();
System.out.println("Random number: " + random);

Output:

Random number: 0.7064755872309822

You can visit the post how to generate a random number in Java which provides a detailed overview of random number generation.

Conclusion

In this post, we learned various methods available in Math class. Math class in Java is a very important class that helps us to do basic numerical operations.

Sharing Is Caring: