java.lang.Math Class in Java, Static Import
java.lang.Math Class
java lang Math Class
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.
java.lang.Math Class Example
/* How to convert negative number to positive number How to get random number, log of a number Save with file name MathExample.java */ public class MathExample { public static void main(String args[]) { // IF THE VALUE IS NEGITIVE IT RETURNS POSITIVE VALUE System.out.println("Absolute : " + Math.abs(-100.50)); // IT INCREASE THE VALUE TO NEAREST INTEGER System.out.println("ceil : " + Math.ceil(100.55)); // IT DECREASE THE VALUE TO NEAREST INTEGER System.out.println("floor : " + Math.floor(100.55)); // IT RETURNS MAX NUMBER WITH IN THE GIVEN NUMBERS System.out.println("max : " + Math.max(100,200)); // IT RETURNS MIN NUMBER WITH IN THE GIVEN NUMBERS System.out.println("min : " + Math.min(100,200)); // IT RETURNS RANDOM NUMBER System.out.println("random : " + Math.random()); // IT RETURNS WITHOUT FLOATING POINT System.out.println("round : " + Math.round(100.75)); // IT RETURNS SQUARE ROOT System.out.println("sqrt : " + Math.sqrt(2)); // IT RETURNS PI VALUE (22/7) System.out.println("PI : " + Math.PI); // RETURNS NATURAL LOGARITHM System.out.println("log : " + Math.log(10.55)); // RETURNS BASE 10 LOGARITHM System.out.println("log10 : " + Math.log10(10.55)); // RETURNS NATURAL LOGARITHM OF SUM OF THE ARGUMENT AND 1 System.out.println("log1p : " + Math.log1p(10.55)); } }
Following Java Math class example you can learn how to use trigonometric functions in java.
How to get Trigonometric values using Java
/* How to get Trigonometric values using Java Example Save with file name MathExample2.java */ public class MathExample2 { public static void main(String args[]) { // RETURNS ARC COSINE System.out.println("acos : " + Math.acos(0.4)); // RETURNS ARC SINE System.out.println("asin : " + Math.asin(0.4)); // RETURNS ARC TANGENT VALUE System.out.println("atan : " + Math.atan(45)); // RETURNS COSINE OF AN ANGLE System.out.println("cos : " + Math.cos(45)); // RETURNS HYPERBOLIC COSINE System.out.println("cosh : " + Math.cosh(45)); // RETURNS SINE OF AN ANGLE System.out.println("sin : " + Math.sin(45)); // RETURNS HYPERBOLIC SINE System.out.println("sinh : " + Math.sinh(45)); // RETURNS TANGENT OF AN ANGLE System.out.println("tan : " + Math.tan(45)); // RETURNS HYPERBOLIC TANGENT System.out.println("tanh : " + Math.tanh(45)); // RETURNS DEGREES System.out.println("toDegrees : " + Math.toDegrees(45)); // RETURNS RADIANS System.out.println("toRadians : " + Math.toRadians(45)); } }