Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Math Class

The Math class in Java provides several utility methods and constants for performing common mathematical operations. It is part of the java.lang package, so you don't need to import it explicitly. Here's a tutorial on using some of the most commonly used methods and constants of the Java Math class.

  • Constants:

The Math class has two commonly used constants:

  • Math.PI: The value of �� (approximately 3.141592653589793).
  • Math.E: The base of the natural logarithm, e (approximately 2.718281828459045).

Example:

System.out.println("Value of PI: " + Math.PI); // Output: Value of PI: 3.141592653589793
System.out.println("Value of E: " + Math.E);   // Output: Value of E: 2.718281828459045
  • Trigonometric methods:

The Math class provides methods for performing trigonometric operations like sine, cosine, tangent, and their inverse functions.

  • Math.sin(double a): Returns the sine of the angle a in radians.
  • Math.cos(double a): Returns the cosine of the angle a in radians.
  • Math.tan(double a): Returns the tangent of the angle a in radians.
  • Math.asin(double a): Returns the arcsine of the value a, in the range of -��/2 through ��/2.
  • Math.acos(double a): Returns the arccosine of the value a, in the range of 0.0 through ��.
  • Math.atan(double a): Returns the arctangent of the value a, in the range of -��/2 through ��/2.

Example:

double angle = Math.PI / 4;

System.out.println("sin(45��): " + Math.sin(angle)); // Output: sin(45��): 0.7071067811865476
System.out.println("cos(45��): " + Math.cos(angle)); // Output: cos(45��): 0.7071067811865476
System.out.println("tan(45��): " + Math.tan(angle)); // Output: tan(45��): 0.9999999999999999
  • Exponential and logarithmic methods:

The Math class provides methods for performing exponential and logarithmic operations.

  • Math.exp(double a): Returns Euler's number, e, raised to the power of a.
  • Math.log(double a): Returns the natural logarithm (base e) of a.
  • Math.log10(double a): Returns the base 10 logarithm of a.
  • Math.pow(double a, double b): Returns the value of a raised to the power of b.

Example:

System.out.println("e^2: " + Math.exp(2));          // Output: e^2: 7.38905609893065
System.out.println("log(e^2): " + Math.log(Math.E * Math.E)); // Output: log(e^2): 2.0
System.out.println("log10(100): " + Math.log10(100)); // Output: log10(100): 2.0
System.out.println("2^3: " + Math.pow(2, 3));       // Output: 2^3: 8.0
  1. Performing basic arithmetic operations in Java using Math class:

    • The Math class includes methods for basic arithmetic operations like addition, subtraction, multiplication, and division.
    int sum = Math.addExact(3, 5); // Returns the sum of two integers
    
  2. Trigonometric and exponential functions in Java Math:

    • Trigonometric functions like sin, cos, and tan, as well as exponential functions like exp and log, are available.
    double sineValue = Math.sin(Math.PI / 4); // Returns the sine of 45 degrees
    
  3. Random number generation with Java Math class:

    • The Math.random() method generates a pseudo-random double value between 0.0 (inclusive) and 1.0 (exclusive).
    double randomNumber = Math.random(); // Generates a random value between 0.0 and 1.0
    
  4. Rounding and precision with Math class in Java:

    • Methods like round, ceil, and floor help with rounding operations.
    double roundedValue = Math.round(5.67); // Rounds to the nearest whole number
    
  5. Power and square root functions in Java Math:

    • Use pow for exponentiation and sqrt for square root calculations.
    double powerResult = Math.pow(2, 3); // Calculates 2^3
    double squareRoot = Math.sqrt(25); // Calculates the square root of 25
    
  6. Using Math class for numerical calculations in Java:

    • The Math class is widely used for various numerical calculations.
    double result = Math.sin(30) + Math.pow(2, 4) / Math.sqrt(9);
    
  7. Handling NaN and Infinity in Java Math:

    • The Math class provides constants like NaN and POSITIVE_INFINITY to represent Not-a-Number and positive infinity, respectively.
    double result = Math.sqrt(-1); // Returns NaN for imaginary square root