java.lang.Integer toString()

Description :

This java tutorial shows how to use the toString() method of Integer class under java.lang package. This method return a string equivalent of the Integer object. This would yield the same result as Integer.toString(int i). This method override the toString() method of Object class.

Method Syntax :

public String toString()

Parameter Input :

DataType Parameter Description
int i the integer value that we want to convert into string in octal base number

Method Returns :

The tooString() method simply returns the string equivalent of the Integer object in base 10.

Compatibility Version :

Requires Java 1.0 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of toString() method of Integer class. Basically we ask for an int input from the console, and then we use the scanner to get the value. After that we assign the value to an Integer wrapper class and then get the string equivalent using the toString() method of Integer class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

import static java.lang.System.*;

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

public class IntegerToString {

	public static void main(String[] args) {
		// Ask user input
		System.out.print("Enter Desired Value:");
		// declare the scanner object
		Scanner scan = new Scanner(System.in);
		// use scanner to get the value from user console
		int intValue = scan.nextInt();
		// close the scanner object
		scan.close();
		Integer myValue = new Integer(intValue);
		String value = myValue.toString();
		out.println("String Value is " + value);

	}

}

Sample Output :

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

Enter Desired Value:126
String Value is 126

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :