Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment

Back to Classroom

Basic Methods

Introduction To Java Course

Engineers From These Top Companies and Universities Trust EXLskills

1M+ Professionals | 100+ Institutions

This is the EXLskills free and open-source Introduction To Java Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of essential coding proficiency.

Is this course FREE?

Yes, this a 100% free course that you can contribute to on GitHub here!

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.