Java Math

Java Math class is a part of the Java Development Kit (JDK) and provides a set of mathematical operations and methods to perform various mathematical calculations. This class is in the java.lang package and doesn't require any import statement.

Math class provides various methods for mathematical operations like absolute value, square root, power, and trigonometric operations like sin, cos, and tan. It also provides some commonly used mathematical constants such as Pi and E.

Important Methods and Constants

Here are some of the most important methods and constants of the Java Math class:

Constants:

  • Math.E : Euler's number, the base of natural logarithms.
  • Math.PI : Pi, the ratio of the circumference of a circle to its diameter.

Absolute Value:

  • Math.abs(x) : returns the absolute value of x.

Example:

int x = -10;
System.out.println("Absolute value of -10 is: "+Math.abs(x));

Output:

Absolute value of -10 is: 10

Rounding:

  • Math.round(x) : returns the closest long to the argument.
  • Math.ceil(x) : returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
  • Math.floor(x) : returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.

Example:

double x = 5.67;
System.out.println("Round off of 5.67: "+Math.round(x));
System.out.println("Ceiling of 5.67: "+Math.ceil(x));
System.out.println("Floor of 5.67: "+Math.floor(x));

Output:

Round off of 5.67: 6
Ceiling of 5.67: 6.0
Floor of 5.67: 5.0

Maximum and Minimum:

  • Math.max(a, b) : returns the greater of two int values.
  • Math.min(a, b) : returns the smaller of two int values.

Example:

int a = 10, b = 20;
System.out.println("Maximum of 10 and 20 is: "+Math.max(a, b));
System.out.println("Minimum of 10 and 20 is: "+Math.min(a, b));

Output:

Maximum of 10 and 20 is: 20
Minimum of 10 and 20 is: 10

Power:

  • Math.pow(a, b): returns the value of the first argument raised to the power of the second argument.

Example:

double a = 5.0, b = 2.0;
System.out.println("5 raised to the power 2: "+Math.pow(a, b));

Output:

5 raised to the power 2: 25.0

Square Root:

  • Math.sqrt(x) : returns the square root of x.

Example:

double x = 25.0;
System.out.println("Square root of 25.0: "+Math.sqrt(x));

Output:

Square root of 25.0: 5.0

Trigonometric Functions:

  • Math.sin(x) : returns the sine of the angle x (x is in radians).
  • Math.cos(x) : returns the cosine of the angle x (x is in radians).
  • Math.tan(x) : returns the tangent of the angle x (x is in radians).

Example:

double x = Math.PI/4;
System.out.println("Sine of PI/4: "+Math.sin(x));
System.out.println("Cosine of PI/4: "+Math.cos(x));
System.out.println("Tangent of PI/4: "+Math.tan(x));

Output:

Sine of PI/4: 0.7071067811865475
Cosine of PI/4: 0.7071067811865475
Tangent of PI/4: 1.0

Conclusion

In conclusion, the Java Math class provides a variety of mathematical operations and methods that are commonly used in programming.

By using the methods and constants of this class, you can easily perform mathematical operations without having to write your own functions.

Java Basics