Java BigInteger Class | toString() Method with Example

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

BigInteger Class toString() method

Syntax:

    public String toString();
    public String toString(int radix's);
  • toString() method is available in java.math package.
  • toString() method is used to represent a decimal string of this BigInteger.
  • toString(int radix's) method is used to represent a string of this BigInteger in the given radix's value.
  • These methods don't throw an exception at the time of representing a string.
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, toString()
    • None
  • In the second case, toString(int radix's)
    • int radix's – represents the radix used to denote this BigInteger as a String.

Return value:

In both the cases, the return type of the method is String,

  • In the first case, it returns the decimal string denotation of this BigInteger.
  • In the second case, it returns string denotation of this BigInteger in the given radix.

Example:

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

import java.math.*;

public class ToStringOfBI {
    public static void main(String args[]) {
        // Initializes a variables str
        String str = "10";
        int radix1 = 10;
        int radix2 = 2;

        // Initializes a BigInteger object  
        BigInteger b_int = new BigInteger(str);

        // Here, this method toString() method is 
        // used to represent this BigInteger b_int value as
        // a String 
        String str_conv = b_int.toString();
        System.out.println("b_int.toString(): " + str_conv);

        // Here, this method toString(int) method is 
        // used to represent this BigInteger b_int value as
        // a String in the given radix "10" i.e. 10
        // means decimal representation of b_int 
        str_conv = b_int.toString(radix1);
        System.out.println("b_int.toString(radix1): " + str_conv);

        // Here, this method toString(int) method is 
        // used to represent this BigInteger b_int value as
        // a String in the given radix "2" i.e. 2
        // means binary representation of b_int 
        str_conv = b_int.toString(radix2);
        System.out.println("b_int.toString(radix2): " + str_conv);
    }
}

Output

b_int.toString(): 10
b_int.toString(radix1): 10
b_int.toString(radix2): 1010


Comments and Discussions!

Load comments ↻





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