Java Basics:Java Library Methods

Java Math Methods

Java has a lot of Math methods that allow us to perform mathematical computations. In this reference page, you will find all the math methods available in Java. For example, if you need to get the square root of a number, use the sqrt() method.


Java Math abs()

The Java Math abs() method returns the absolute value of the specified value.

The syntax of the abs() method is:

Math.abs(num)

Here, abs() is a static method. Hence, we are accessing the method using the class name, Math.


abs() Parameters

The abs() method takes a single parameter.

  • num - number whose absolute value is to be returned. The number can be:
    • int
    • double
    • float
    • long

abs() Return Value

  • returns the absolute value of the specified number
  • returns the positive value if the specified number is negative

Example 1: Java Math abs() with Positive Numbers

import java.lang.Math;

class Main {
  public static void main(String[] args) {

    // create variables
    int a = 7;
    long b = 23333343;
    double c = 9.6777777;
    float d = 9.9f;

    // print the absolute value
    System.out.println(Math.abs(a));  // 7
    System.out.println(Math.abs(c));  // 9.6777777


    // print the value without negative sign
    System.out.println(Math.abs(b));  // 23333343
    System.out.println(Math.abs(d));  // 9.9
  }
}

In the above example, we have imported the java.lang.Math package. This is important if we want to use methods of the Math class. Notice the expression,

Math.abs(a)

Here, we have directly used the class name to call the method. It is because abs() is a static method.


Example 2: Java Math abs() with Negative Numbers

import java.lang.Math;

class Main {
  public static void main(String[] args) {

    // create variables
    int a = -35;
    long b = -141224423L;
    double c = -9.6777777d;
    float d = -7.7f;

    // get the absolute value
    System.out.println(Math.abs(a));  // 35
    System.out.println(Math.abs(b));  // 141224423
    System.out.println(Math.abs(c));  // 9.6777777
    System.out.println(Math.abs(d));  // 7.7
  }
}

Here, we can see that the abs() method converts the negative value into a positive value.

____________________________________________________________

Java Math acos()

The Java Math acos() method returns the arc cosine of the specified value.

The arc cosine is the inverse of the cosine function.

The syntax of the acos() method is:

Math.acos(double num)

Here, acos() is a static method. Hence, we are accessing the method using the class name, Math.


acos() Parameters

The acos() method takes a single parameter.

  • num - number whose arc cosine is to be returned. It should be always less than 1.

acos() Return Value

  • returns the arc cosine of the specified number
  • returns NaN if the specified number is NaN or greater than 1

Note: The returned value is an angle between 0.0 to pi.


Example 1: Java Math acos()

import java.lang.Math;

class Main {
  public static void main(String[] args) {

    // create variable
    double a = 0.5;
    double b = 0.79;
    double c = 0.0;

    // print the arc cosine value
    System.out.println(Math.acos(a));  // 1.0471975511965979
    System.out.println(Math.acos(b));  // 0.6599873293874984
    System.out.println(Math.acos(c));  // 1.5707963267948966
  }
}

In the above example, we have imported the java.lang.Math package. This is important if we want to use methods of the Math class. Notice the expression,

Math.acos(a)

Here, we have directly used the class name to call the method. It is because acos() is a static method.


Example 2: Math acos() Returns NaN

import java.lang.Math;

class Main {
  public static void main(String[] args) {

    // create variable
    double a = 2;

    // square root of negative number
    // results in not a number (NaN)
    double NaN = Math.sqrt(-5);

    // print the arc cosine value
    System.out.println(Math.acos(a));  // NaN
    System.out.println(Math.acos(NaN));  // NaN
  }
}

Here, we have created two variables named a and b.

  • Math.acos(a) - returns NaN because value of a is greater than 1.
  • Math.acos(b) - returns NaN because square root of a negative number (-5) is not a number.

Note: We have used the Java Math.sqrt() method to compute the square root of a number.

Java Math addExact()

The Java Math addExact() method adds the specified numbers and returns it.

The syntax of the addExact() method is:

Math.addExact(num1, num2)

Here, addExact() is a static method. Hence, we are accessing the method using the class name, Math.


addExact() Parameters

The addExact() method takes two parameters.

  • num1 - value that is added to num2
  • num2 - value that is added to num1

Note: The data type of both values should be either int or long.


addExact() Return Value

  • returns the sum of two values

Example 1: Java Math addExact()

import java.lang.Math;

class Main {
  public static void main(String[] args) {

    // create int variable
    int a = 24;
    int b = 33;

    // addExact() with int arguments
    System.out.println(Math.addExact(a, b));  // 57

    // create long variable
    long c = 12345678l;
    long d = 987654321l;

    // addExact() with long arguments
    System.out.println(Math.addExact(c, d));  // 999999999
  }
}

In the above example, we have used the Math.addExact() method with the int and long variables to calculate the sum.


The addExact() method throws an exception if the result of addition overflows the data type. That is, the result should be within the range of the data type of specified variables.

import java.lang.Math;

class Main {
  public static void main(String[] args) {

    // create int variable
    // maximum int value
    int a = 2147483647;
    int b = 1;

    // addExact() with int arguments
    // throws exception
    System.out.println(Math.addExact(a, b));
  }
}

In the above example, the value of a is the maximum int value and the value of b is 1. When we add a and b,

  2147483647 + 1
=> 2147483648    // out of range of int type     

Hence, the addExact() method throws the integer overflow exception.

  • ________________________________-________________________

Java Math asin()

The Java Math asin() method returns the arc sine of the specified value.

The arcsine is the inverse of the sine function.

The syntax of the asin() method is:

Math.asin(double num)

Here, asin() is a static method. Hence, we are accessing the method using the class name, Math.


asin() Parameters

The asin() method takes a single parameter.

  • num - number whose arc sine is to be returned

Note: The absolute value of num should be always less than 1.


asin() Return Value

  • returns the arcsine of the specified number
  • returns 0 if the specified value is zero
  • returns NaN if the specified number is NaN or greater than 1

Note: The returned value is an angle between -pi/2 to pi/2.


Example 1: Java Math asin()

import java.lang.Math;

class Main {
  public static void main(String[] args) {

    // create variable
    double a = 0.99;
    double b = 0.71;
    double c = 0.0;

    // print the arcsine value
    System.out.println(Math.asin(a));  // 1.4292568534704693
    System.out.println(Math.asin(b));  // 0.7812981174487247
    System.out.println(Math.asin(c));  // 0.0
  }
}

In the above example, we have imported the java.lang.Math package. This is important if we want to use methods of the Math class. Notice the expression,

Math.asin(a)

Here, we have directly used the class name to call the method. It is because asin() is a static method.


Example 2: Math asin() Returns NaN

import java.lang.Math;

class Main {
  public static void main(String[] args) {

    // create variable
    double a = 2;

    // square root of negative number
    // results in not a number (NaN)
    double b = Math.sqrt(-5);

    // print the arc sine  value
    System.out.println(Math.asin(a));  // NaN
    System.out.println(Math.asin(b);  // NaN
  }
}

Here, we have created two variables named a and b.

  • Math.asin(a) - returns NaN because value of a is greater than 1
  • Math.asin(b) - returns NaN because square root of a negative number (-5) is not a number

Note: We have used the Java Math.sqrt() method to compute the square root of a number.

Comments

Popular posts from this blog

Java OOPS:OOPS Concepts Overview

Selenium-Java Contents

Java OOPS:Constructors in Java – A complete study!!