java.lang.Integer toString(int i, int radix)

Description :

This java tutorial shows how to use the toString(int i, int radix) method of Integer class under java.lang package. This method returns a string equivalent of the the int method argument with consideration the on the radix specified.

Method Syntax :

public static String toString(int i,int radix)

Parameter Input :

DataType Parameter Description
int i the int value that we want to convert
int radix the radix or base to be used in getting the string representation of int method argument

Method Returns :

This method simply returns the string representation of the int method argument with respect to the radix which is commonly called base number as specified.

Compatibility Version :

Requires Java 1.0 and up

Exception :

None

Discussion :

This method basically can do base number conversion. Like for example we want to convert a number to base 5, we just have to invoke Integer.toString(number,5). The returned string will be the number equivalent to base 5. Since the toString method is static, thus it is required to call it statically.

If the maximum and minimum value allowable has been breached, the radix will be defaulted to 10 which signifies the base number of counting numbers. The maximum value can be determined using

The radix maximum value is determined using Character.MAX_RADIX while the minimum is determined using Character.MIN_RADIX.

Java Code Example :

This java example source code demonstrates the use of toString(int i, int radix) method of Integer class. Basically we have asked for a number from user input on the console and then convert the same into a number on base 16.

package com.javatutorialhq.java.examples;

/* * This java example shows the static method * of Integer class toString(int i, int radix) * Basically this example just convert an int to a number in base 16 */

public class IntegerToStringIntRadix {

public static void main(String

[] args) { // initialize int value and target radix int numberValue = 35; int baseNumber = 16; // convert value to hex String numberValueHex = Integer.toString(numberValue, baseNumber); // printing desired results System.out.println(numberValue + " in radix " + baseNumber + " is " + numberValueHex); }

}

Sample Output :

Running the toString(int i, int radix) method example source code of Integer class class will give you the following output

35 in radix 16 is 23

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :