Java.lang - SPLessons

Java.lang Math Class

Home > Lesson > Chapter 9
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.lang Math Class

Java.lang Math Class

shape Description

The functionality of java.lang.Math class is to perform basic operations such as addition, subtraction, square root, logarithmic and trigonometric functions. Following is the syntax declaration of this class. [java] public final class Math extends Object [/java] Following are the basic methods of java.lang.Math class.
Methods Description
abs(double) Absolute value of a double value will be returned.
abs(float) Absolute value of a float value will be returned.
abs(int) Absolute value of a int value will be returned.
acos(double) To return the cosine of a value, in the range of 0.0 through pi.
asin(double) To return the sine of a value, in the range of-pi/2 through pi/2.
ceil(double) To return the smallest double value.
floor(double) To return the greatest double value.
sqrt(double) To return the square root for the double value.

shape Example

By using java.lang.Math.abs(double a) Method

DemoMath.java [java] package com.SPlessons; import java.lang.Math; public class DemoMath { public static void main(String[] args) { // get some doubles to find their absolute values double a = 1234.1874d; double b = -0123.0d; // get and print their absolute values System.out.println("Math.abs(" + a + ")=" + Math.abs(a)); System.out.println("Math.abs(" + b + ")=" + Math.abs(b)); System.out.println("Math.abs(-9999.555d)=" + Math.abs(-5555.666d)); } } [/java] The functionality of this method is to return absolute value. Here if the value is zero( either negative or positive) then it returns positive zero. Output: Following is the result will be generated. [java] Math.abs(1234.1874)=1234.1874 Math.abs(-123.0)=123.0 Math.abs(-9999.555d)=5555.666 [/java]

By using Java.lang.Math.acos() Method

DemoMath.java [java] package com.SPlessons; import java.lang.Math; public class DemoMath { public static void main(String[] args) { // get a variable d which is equal to PI/2 double d = Math.PI / 2; // convert d to radians d = Math.toRadians(d); // get the arc cosine of x System.out.println("Math.acos(" + d + ") = " + Math.acos(d)); } } [/java] Here if the value is NaN(Not a Number) then the result is NaN, here the purpose of java.lang.Math.toRadians is to change over a point measured in degrees to a roughly proportional edge measured in radians. Output: The result will be as follows. [java] Math.acos(0.027415567780803774) = 1.5433773235341761 [/java]

By using Java.lang.Math.ceil() Method

DemoMath.java [java] package com.SPlessons; import java.lang.Math; public class DemoMath { public static void main(String[] args) { // get two double numbers double m = 265.9; double n = 0.5878; // call ceal for these these numbers System.out.println("Math.ceil(" + m + ")=" + Math.ceil(m)); System.out.println("Math.ceil(" + n + ")=" + Math.ceil(n)); System.out.println("Math.ceil(-0.65)=" + Math.ceil(-0.95)); } } [/java] In case the argument value is same as integer value then result will be as argument value only. Output: The result will be as follows. [java] Math.ceil(265.9)=266.0 Math.ceil(0.5878)=1.0 Math.ceil(-0.65)=-0.0 [/java]

By using Java.lang.Math.floor() Method

Instead of the ceil place the floor in the same code then result will be as follows. Output: The functionality of floor() method is that to return the greatest double value. [java] Math.ceil(265.9)=265.0 Math.ceil(0.5878)=0.0 Math.ceil(-0.65)=-1.0 [/java]

By using Java.lang.Math.sqrt() Method

DemoMath.java [java] package com.SPlessons; import java.lang.Math; public class DemoMath { public static void main(String[] args) { // get two double numbers numbers double m = 16; double n = 144; // print the square root of these doubles System.out.println("Math.sqrt(" + m + ")=" + Math.sqrt(m)); System.out.println("Math.sqrt(" + n + ")=" + Math.sqrt(n)); } } [/java] Here if the argument is negative or positive then the result will be same as the argument. If the argument is less than zero then result will be NaN(Not a Number). Output: The result will be as follows. [java] Math.sqrt(16.0)=4.0 Math.sqrt(144.0)=12.0 [/java]

Summary

shape Key Points

  • The cos(double a) method is used to return the trigonometric cosine of an angle.
  • The java.lang.Math class also will hire the methods from java.lang.Object class.
  • The value of one radian equals to 57.2958 degrees.