ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to convert Integer to String in Java?

Updated on November 21, 2013

For enterprise java application or programming, it is a common thing that a java developer or programmer needs to convert integer to string. Using Java classes, it is very easy and straight forward to convert an integer to a string.

Steps to convert Integer to String in Java:

Step 1: Integer variable declaration

First, define an integer variable that you need to convert. Suppose, we have a integer "123". We will define that value using a variable like myInt. So, the full expression will be:

int myInt = 123;

Step 2: String variable declaration

After converting the integer value to string, we need to save the that value into a variable so that we could use that later. Moreover, the data type of this variable should be String type. For example, name of the String variable is myString. The total expression to define this string would be like the following:

String myString;

Conversion from integer to string:

Using Java, you can easily convert the integer to string. To convert this, we will use toString() method which is available in Integer class. So, we will convert the integer and save it into the string variable which we declared previous step. The code for this will look like the following line:

myString = Integer.toString(myInt);

Printing the variable into the console:

To test whether the conversion is correct or wrong, we can check this by printing that variable into the console. The following line of code is used to print the string variable:

System.out.println("String output: " + myString)

Sample full Java code:

Below is a sample program which converts a integer to string. Using Java this code is implemented.

public class ConvertingIntegerToString {

	public static void main(String[] args) {

		int myInt = 123;
		String myString;

		myString = Integer.toString(myInt);

		System.out.println("Integer output: " + myInt);
		System.out.println("String output: " + myString);

	}

}

Screenshot of the program with sample output

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article