Basic Methods

Math Class

Basic Methods

🙋 Need help? Ask an expert now!

The Java Math class contains all the familiar mathematical functions found on an electronic calculators, such as sine, log, and square root and more. For the most part, the functions use datatype double as a parameter and returns a double value. Let's take a look at a few of the methods and fields in the Math class.

MathClassExample.java
package exlcode;

public class MathClassExample {

  public static double exampleVariableOne = Math.PI;
  // Methods will be explained later this unit
  public static double exampleVariableTwo = Math.pow(10, 2);
  public static double exampleVariableThree = Math.sqrt(exampleVariableTwo);

  public static void main(String[] args) {
    System.out.println(exampleVariableOne);
    System.out.println(exampleVariableTwo);
    System.out.println(exampleVariableThree);
  }
}

We will go into further detail about the methods of the Math class later on, so let's explore Math.PI, one of the two fields of the Math class. This field represents the value of pi. A dot is used after typing "Math" to access the field and the method inside the Math class. You have to check the input and output type of the methods inside the Math class to use them correctly without throwing errors. To avoid syntax errors, the method and field names have to match the names written in the Java documentation for the Math class. You do not have to import the Math class as it is already part of the java.lang package.

Edit Me on GitHub!

Application Question

Which of the following is NOT a valid use of the Math class?