Java.lang Package Classes

Java Integer - toString() Method



The java.lang.Integer.toString() method returns a string representation of the first argument in the radix specified by the second argument. If the radix is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX, then the radix 10 is used instead.

Syntax

public static String toString(int i,
                              int radix)

Parameters

i Specify the integer to be converted to a string.
radix Specify the radix to use in the string representation.

Return Value

Returns a string representation of the argument in the specified radix.

Exception

NA.

Example:

In the example below, the java.lang.Integer.toString() method returns a String object representing the given integer using the specified radix.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating a int value
    int x = 111;

    //printing the int value 
    System.out.println("The int value is: " + x); 

    //creating and printing the string representation
    //of the int value using different radix
    System.out.println("The x in binary is: " + Integer.toString(x, 2));
    System.out.println("The x in octal is: " + Integer.toString(x, 8)); 
    System.out.println("The x in hexadecimal is: " + Integer.toString(x, 16));    
  }
}

The output of the above code will be:

The int value is: 111
The x in binary is: 1101111
The x in octal is: 157
The x in hexadecimal is: 6f

❮ Java.lang - Integer

5