How to Convert Int to String in Java - ValueOf Method

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

This lesson covers an important tool in the Java toolbox, converting integer values to a String data type. The valueOf method will be used and working code examples provided as we explore this feature.


Java allows you to convert values from one type to another, given certain conditions. We can convert a string/text variable to a numeric value, but only if the string value is numeric. When converting from a numeric to a string value, however, we have a little more leeway. This is because a String can contain both numeric and text values.

ValueOf Method

We will use the valueOf method in Java to convert an int to a String. This method is actually built within the Java String class. Recall that Java not only lets you declare primitive data types (int, double, float), but it stores those in classes: Integer, Double, and String. The valueOf method accepts one parameter, the integer value we are converting.

Let's look at some code. We can either pass in a value directly to the method or a variable. We'll start with an actual value:

String convertMe = String.valueOf(15);

Next, let's create a variable and pass the variable to the valueOf method.

int empID = 1534; String myString = String.valueOf(empID); System.out.println(myString);

Now that we understand the concept, we can take it up another level. We will accept user input from the keyboard and convert the integer value to a String.

Scanner input = new Scanner(; System.out.println("Enter Integer: "); int myInput = input.nextInt(); String myString = String.valueOf(myInput);
System.out.println("Your Int, " + myInput + "Is a String: " + myString);

But wait! What if the user types in 15.234398 in the integer value? We know that a String can contain letters, numbers, and all other types of information. But an integer cannot. If the user enters a non-integer value, the program will crash with a message similar to the following:

Java valueof exception

This is an input exception. Java tried to convert a non-integer to a String. Java would be OK with the conversion if it wasn't an integer. But, we declared it as an integer!

Incorporating a Try and Catch Block

The solution is to wrap the valueOf commands within a try and catch block. This method of coding tries to carry out some instructions. You provide the fail-over code between the brackets of the catch statement. This will trap any errors and display output of our choosing, instead of the red error from Java.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account