WikiGalaxy

Personalize

Java Math: Introduction

Overview:

The Java Math class provides a collection of methods for performing basic numeric operations such as exponentiation, logarithms, square roots, and trigonometric functions. These methods are static, allowing them to be called without creating an instance of the Math class.

Math.abs()

Purpose:

The Math.abs() method returns the absolute value of a given number. It can handle different types of numeric data, including int, long, float, and double.

Example Usage:


public class MathExample {
  public static void main(String[] args) {
    System.out.println(Math.abs(-10)); // Output: 10
    System.out.println(Math.abs(5.5)); // Output: 5.5
  }
}
      

Math.max() and Math.min()

Purpose:

Math.max() returns the greater of two values, while Math.min() returns the smaller. These methods are useful in scenarios where comparisons between two values are necessary.

Example Usage:


public class MathExample {
  public static void main(String[] args) {
    System.out.println(Math.max(10, 20)); // Output: 20
    System.out.println(Math.min(10, 20)); // Output: 10
  }
}
      

Math.sqrt()

Purpose:

The Math.sqrt() method calculates the square root of a number. It is particularly useful in mathematical computations where determining the root of a number is required.

Example Usage:


public class MathExample {
  public static void main(String[] args) {
    System.out.println(Math.sqrt(16)); // Output: 4.0
    System.out.println(Math.sqrt(2));  // Output: 1.4142135623730951
  }
}
      

Math.pow()

Purpose:

The Math.pow() method raises a base to the power of an exponent, providing a way to perform exponential calculations.

Example Usage:


public class MathExample {
  public static void main(String[] args) {
    System.out.println(Math.pow(2, 3)); // Output: 8.0
    System.out.println(Math.pow(5, 2)); // Output: 25.0
  }
}
      

Math.random()

Purpose:

The Math.random() method generates a random double value greater than or equal to 0.0 and less than 1.0. It's often used in applications requiring random number generation.

Example Usage:


public class MathExample {
  public static void main(String[] args) {
    System.out.println(Math.random()); // Output: A random number between 0.0 and 1.0
  }
}
      

Math.ceil() and Math.floor()

Purpose:

Math.ceil() rounds a number up to the nearest integer, while Math.floor() rounds down to the nearest integer. These methods are useful in situations where precise control over rounding behavior is needed.

Example Usage:


public class MathExample {
  public static void main(String[] args) {
    System.out.println(Math.ceil(4.3));  // Output: 5.0
    System.out.println(Math.floor(4.7)); // Output: 4.0
  }
}
      

Math.round()

Purpose:

The Math.round() method rounds a floating-point number to the nearest integer. It is particularly useful when exact whole numbers are required from floating-point calculations.

Example Usage:


public class MathExample {
  public static void main(String[] args) {
    System.out.println(Math.round(4.5)); // Output: 5
    System.out.println(Math.round(4.4)); // Output: 4
  }
}
      

Math.log() and Math.log10()

Purpose:

Math.log() returns the natural logarithm (base e) of a double value, while Math.log10() returns the base 10 logarithm. These methods are essential in scientific calculations involving exponential growth or decay.

Example Usage:


public class MathExample {
  public static void main(String[] args) {
    System.out.println(Math.log(1));     // Output: 0.0
    System.out.println(Math.log10(100)); // Output: 2.0
  }
}
      

Math.sin(), Math.cos(), and Math.tan()

Purpose:

These trigonometric methods calculate the sine, cosine, and tangent of an angle (in radians), respectively. They are widely used in fields such as physics and engineering.

Example Usage:


public class MathExample {
  public static void main(String[] args) {
    System.out.println(Math.sin(Math.PI / 2)); // Output: 1.0
    System.out.println(Math.cos(0));           // Output: 1.0
    System.out.println(Math.tan(Math.PI / 4)); // Output: 1.0
  }
}
      
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025