java lang Math Class - java.lang.Math Class in Java, Static Import
Math class in Java
Java Math Class
The Java Math class contains methods for performing basic numeric operations such as the exponential, logarithm, square root, and trigonometric functions. Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict reproducibility is not required.
In this tutorial you can learn how to use Math class. You need not to create instance for Math class. Because all the methods in Math class are static methods. All the static methods are called with the class name.
What is java.lang.Math in Java ?
The java.lang.Math is a class in Java . This java.lang.Math class is available in java.lang package . The Java Math class contains methods for performing basic numeric operations such as the elementary exponential , logarithm , square root , and trigonometric functions .
What is the package name which contains Math class ?
The Java package that contains the Math class is java.lang.Math i.e. java.lang package. This Math class is used to perform almost all basic numeric operations . It is defined as public final class Math in Java . In other words java.lang package contains the Math class.
Do I need to import the Math class in Java ?
No, you need not to import the Math class in Java. Since Math class is in the java.lang package , the Math class does not need to be imported . However , in programs extensively utilizing these functions , a static import can be used . There is no need to explicitly import java.lang.Math as its imported implicitly . All its methods are static . This explanation also useful for how to import java.lang.Math ? .
What is static import in Java ?
Static import means that the fields and methods in a class can be used in the code without specifying their class if they are defined as public static. The Math class method sqrt() in the package java.lang is static import as stated in the following example.
For example the static import for sqrt method of Math class in Java is :
import static java.lang.Math.sqrt;
The Math class is not required along with the method sqrt() as static import is used as java.lang.Math.sqrt
How to use Math class methods in Java ?
The Math class exists in the java.lang package. That's why no need to import Math class in your programs . All the Math class methods are static . So you can use Math class methods with class name .
What is the function of java.lang.Math in Java ?
The class java.lang.Math contains methods for performing basic numeric operations such as the elementary exponential , logarithm , square root , and trigonometric functions . To help ensure portability of Java programs , the definitions of many of the numeric functions in this package require that they produce the same results as certain published algorithms. The following are the methods or functions of Math class in Java .
What are the Java Math Operators ?
Java contains a set of math operators for performing simple math operations on Java variables. The Java math operators are simple to use in mathematical operations.
Math Operators in Java
- + operator is used in mathematical operations, for performing addition.
- - operator is used in mathematical operations, for performing subtraction.
- * operator is used in mathematical operations, for performing multiplication.
- / operator is used in mathematical operations, for performing division.
- % operator is used in mathematical operations, for performing madulo division.
Each of these math operators explained in more detail in the Java Operators Tutorial.
What are the Math class methods in Java ?
java.lang.Math Class methods helps to perform the numeric operations like square, square root, cube, cube root, exponential and trigonometric operations . Following are the List of java.lang.Math Class Methods or functions .
Basic Math Methods or Functions
|Math.abs()||java.lang.Math.abs() method returns the absolute value of any type of argument passed. This method can handle all the data types.|
|Math.acos()||java.lang.Math.acos() method returns the arc cosine value of the passed argument.|
|Math.toRadians()||java.lang.Math.toRadians(double deg) method converts argument (degree) to radians.|
|Math.asin()||java.lang.Math.asin() method returns the arc sine value of the method argument passed.|
|Math.cbrt()||java.lang.Math.cbrt() method returns the cube root of the passed argument.|
|Math.floor()||java.lang.Math.floor() method returns the floor value of an argument i.e. the closest integer value which is either less or equal to the the passed argument.|
|Math.hypot()||java.lang.Math.hypot(double p, double b) method returns hypotenuse of a right triangle on passing the traingle’s base and perpendicular as arguments.|
|Math.IEEEremainder()||java.lang.Math.IEEERemainder(double d1, double d2) method returns the remainder value by applying remainder operation on two arguments w.r.t IEEE 754 standard.|
|Math.log()||java.lang.Math.log() method returns the logarithmic value of the passed argument.|
|Math.ceil()||java.lang.Math.ceil(double a) method returns the smallest possible value which is either greater or equal to the argument passed. The returned value is a mathematical integer.|
|Math.atan()||java.lang.Math.atan() method returns returns the arc tangent of the method argument value.|
|Math.copySign()||java.lang.Math.copySign() method returns first floating-point argument but having the sign of second argument.|
|Math.cosh()||java.lang.Math.cosh() method returns the hyperbolic cosine of the argument passed.|
|Math.decrementExact()||java.lang.Math.decrementExact() method decrements the value of passed argument by one.|
|Math.exp()||java.lang.Math.exp(double arg) method returns the Euler’s number raised to the power of double argument.|
|Math.incrementExact()||java.lang.Math.incrementExact() method returns the argument by incrementing it’s value.|
|Math.log10()||java.lang.Math.log10() method returns the base10 logarithmic value of the passed argument.|
|Math.pow()||java.lang.Math.pow(double b, double e) method returns the value as be|
|Math.signum()||java.lang.Math.signum() method returns the signum value of the argument passed.|
|Math.round()||java.lang.Math.round() method round off the passed argument upto closest decimal places.|
|Math.max()||java.lang.Math.max(double v1, double v2) method returns the greater value out of the two passed argument values. This method just compares using magnitude without considering any sign.|
|Math.log1p()||java.lang.Math.log1p() method returns natural log of (passed argument + 1).|
|Math.ulp()||java.lang.Math.ulp() method returns Unit of least precision(ulp) i.e. the least distance between two floating point numbers.|
java.lang.Math Class Example
Following Java Math class example you can learn how to use trigonometric functions in java.
How to get Trigonometric values using Java