java.lang.Integer toString(int i)

Description :

This java tutorial shows how to use the toString(int i) method of Integer class under java.lang package. This method return a string equivalent of the int parameter which has been converted into signed decimal format.

Method Syntax :

public static String toString(int i)

Parameter Input :

DataType Parameter Description
int i the integer value that we want to convert into string

Method Returns :

The toString(int i) method simply returns the decimal string equivalent of int method parameter.

Compatibility Version :

Requires Java 1.0 and up

Exception :

N/A

Discussion :

The toString(int i) method is static thus we should invoke it statically for example Integer.toString(int i). This method simply converts the int parameter into decimal or we could call it base 10. Make a note that counting numbers are of base 10, so basically we just convert the int primitive data type into string.

The toString(int i) method would yield the same result in invoking Integer.toString(int i, int radix). This is just only a shortcut of calling the toString(int i,10). You might be asking when to use this method. Well, to answer this kind of concern, if you are only dealing with fix base 10, it would make sense to use the toString(int i) because it is simpler to look at. But if you are dealing with dynamic conversion then the toString(int i, int radix) method suits better.

There is maximum and minimum allowable value to be converted and this is can be determined using the two static method Integer.MAX_VALUE and Integer.MIN_VALUE respectively.

Java Code Example :

This java example source code demonstrates the use of toString(int i) method of Integer class. Basically we asked for the user input on the command shell and then present the same in decimal format.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

import static java.lang.System.*;

/*
 * This example source code demonstrates the use of 
 * toString(int i) method of Integer class
 */

public class IntegerToBinaryString {

	public static void main(String[] args) {
		// Ask user input
		System.out.print("Number:");
		// declare the scanner object
		Scanner scan = new Scanner(System.in);
		// use scanner to get the value from user console
		int value = scan.nextInt();
		// close the scanner object
		scan.close();
		// print the value in decimal format
		out.println("Number in binary base number:" + Integer.toString(value));

	}

}

Sample Output :

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

Number:123
Number in binary base number:123

Exception Scenario :

Exception in thread "main" java.util.InputMismatchException: For input string: "21474836410"
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at com.javatutorialhq.java.examples.IntegerToBinaryString.main(IntegerToBinaryString.java:20)

Similar Method :

  • N/A

Suggested Reading List :

References :