How to convert int to string in Java

Converting an int value into a String is an easy task, simply because no formatting is required. The other direction (string to int) is a bit more complicated.

Convert int value to String

There are several ways to convert int to String. The three ways are more or less equal for me, maybe some people do not like the third solution so much.

For the simple datatype int there is the static valueOf() method in String class.

int i = 50;
String str = String.valueOf(i);

Alternatively, the toString() method of the Integer class can be used:

str = Integer.toString(i);

Another possibility is the “quick & dirty” solution using the plus operator:

s = "" + i;

The int value is simply appended to an empty string, which in turn results in a character string with the numeric value.


Convert integer object to string

It’s easier with an integer object because it’s an object with methods. The toString() method delivers exactly what we need:

Integer i = new Integer(50);
String str = integer.toString();
java-mcq-multiple-choice-questions-and-answersJava MCQ – Multiple Choice Questions and Answers – OOPsThis collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java OOPs”.   1. Which of the…Read More mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *