Java BigInteger Class | abs() Method with Example

BigInteger Class abs() method: Here, we are going to learn about the abs() method of BigInteger Class with its syntax and example.
Submitted by Preeti Jain, on May 10, 2020

BigInteger Class abs() method

  • abs() method is available in java.math package.
  • abs() method is used to get the absolute value of this BigInteger.
  • abs() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • abs() method does not throw an exception at the time of returning the absolute value.

Syntax:

    public BigInteger abs();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is BigInteger, it returns the absolute value of this BigInteger.

Example:

// Java program to demonstrate the example 
// of BigInteger abs() method of BigInteger

import java.math.*;

public class AbsOfBI {
    public static void main(String args[]) {
        // Initialize two variables str1 and str2
        String str1 = "-100";
        String str2 = "2436";

        // Initialize two BigInteger objects 
        BigInteger b_int1 = new BigInteger(str1);
        BigInteger b_int2 = new BigInteger(str2);

        // returns the BigInteger that holds 
        // the absolulte value of this BigInteger
        // b_int1 and store the result in a variable
        // named abs_val
        BigInteger abs_val = b_int1.abs();
        System.out.println("b_int1.abs(): " + abs_val);

        // returns the BigInteger that holds 
        // the absolulte value of this BigInteger
        // b_int2 and store the result in a variable
        // named abs_val
        abs_val = b_int2.abs();
        System.out.println("b_int2.abs(): " + abs_val);
    }
}

Output

b_int1.abs(): 100
b_int2.abs(): 2436


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.