Download java lang Math Class in Formats
QR Code for this Page
java lang Math Class

java lang Math Class

java.lang.Math Class

Math class in Java

The Math class contains methods for performing basic numeric operations such as the elementary 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.

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));
	}
}
Download java lang Math Class in Formats