Java Convert int to String
We can convert int to String in java using String.valueOf() and Integer.toString() methods. Alternatively, we can use String.format() method, string concatenation operator etc.

Scenario
It is generally used if we have to display number in textfield because everything is displayed as a string in form.
1) String.valueOf()
The String.valueOf() method converts int to String. The valueOf() is the static method of String class. The signature of valueOf() method is given below:
Java int to String Example using String.valueOf()
Let’s see the simple code to convert int to String in java.
Let’s see the simple example of converting String to int in java.
300 200100
2) Integer.toString()
The Integer.toString() method converts int to String. The toString() is the static method of Integer class. The signature of toString() method is given below:
Java int to String Example using Integer.toString()
Let’s see the simple code to convert int to String in java using Integer.toString() method.
Let’s see the simple example of converting String to int in java.
300 200100
3) String.format()
The String.format() method is used to format given arguments into String. It is introduced since Jdk 1.5.
Java int to String Example using String.format()
Let’s see the simple code to convert int to String in java using String.format() method.
Next Topic Java String to long

For Videos Join Our Youtube Channel: Join Now
Feedback
- Send your Feedback to [email protected]
Help Others, Please Share

Learn Latest Tutorials
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
Python Design Patterns
![]()
![]()
![]()
Preparation




Trending Technologies
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
B.Tech / MCA
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter

Int to String in Java – How to Convert an Integer into a String

Ihechikara Vincent Abba

You can convert variables from one data type to another in Java using different methods.
In this article, you’ll learn how to convert integers to strings in Java in the following ways:
- Using the Integer.toString() method.
- Using the String.valueOf() method.
- Using the String.format() method.
- Using the DecimalFormat class.
How to Convert an Integer to a String in Java Using Integer.toString()
The Integer.toString() method takes in the integer to be converted as a parameter. Here’s what the syntax looks like:
Integer.toString(INTEGER_VARIABLE)
Here’s an example:
class IntToStr < public static void main(String[] args) < int age = 2; String AGE_AS_STRING = Integer.toString(age); System.out.println("The child is " + AGE_AS_STRING + " years old"); // The child is 2 years old >>
In the example above, we created an integer – age – and assigned a value of 2 to it.
To convert the age variable to a string, we passed it as a parameter to the Integer.toString() method: Integer.toString(age) .
We stored this new string value in a string variable called AGE_AS_STRING .
We then concatenated the new string variable with other strings: «The child is » + AGE_AS_STRING + » years old» .
But, would an error be raised if we just concatenated the age variable to these other strings without any sort of conversion?
class IntToStr < public static void main(String[] args) < int age = 2; System.out.println("The child is " + age + " years old"); // The child is 2 years old >>
The output above is the same as the example where we had to convert the integer to a string.
So how do we know if the type conversion actually worked?
We can check variable types using the Java getClass() object. That is:
class IntToStr < public static void main(String[] args) < int age = 2; String AGE_AS_STRING = Integer.toString(age); System.out.println(((Object)age).getClass().getSimpleName()); // Integer System.out.println(AGE_AS_STRING.getClass().getSimpleName()); // String >>
Now we can verify that when the age variable was created, it was an Integer , and after type conversion, it became a String .
How to Convert an Integer to a String in Java Using String.valueOf()
The String.valueOf() method also takes the variable to be converted to a string as its parameter.
class IntToStr < public static void main(String[] args) < int age = 2; String AGE_AS_STRING = String.valueOf(age); System.out.println("The child is " + AGE_AS_STRING + " years old"); // The child is 2 years old >>
The code above is similar to that in the last section:
- We created an integer called age .
- We passed the age integer as a parameter to the String.valueOf() method: String.valueOf(age) .
You can also check to see if the type conversion worked using the getClass() object:
System.out.println(((Object)age).getClass().getSimpleName()); // Integer System.out.println(AGE_AS_STRING.getClass().getSimpleName()); // String
How to Convert an Integer to a String in Java Using String.format()
The String.format() method takes in two parameters: a format specifier and the variable to be formatted.
Here’s an example:
class IntToStr < public static void main(String[] args) < int age = 2; String AGE_AS_STRING = String.format("%d", age); System.out.println("The child is " + AGE_AS_STRING + " years old"); // The child is 2 years old >>
In the example above, we passed in two parameters to the String.format() method: «%d» and age .
«%d» is a format specifier which denotes that the variable to be formatted is an integer.
age , which is the second parameter, will be converted to a string and stored in the AGE_AS_STRING variable.
You can also check the variable types before and after conversion:
System.out.println(((Object)age).getClass().getSimpleName()); // Integer System.out.println(AGE_AS_STRING.getClass().getSimpleName()); // String
How to Convert an Integer to a String in Java Using DecimalFormat
The DecimalFormat class is used for formatting decimal numbers in Java. You can use it in different ways, but we’ll be using it to convert an integer to a string.
Here’s an example:
import java.text.DecimalFormat; class IntToStr < public static void main(String[] args) < int age = 2; DecimalFormat DFormat = new DecimalFormat("#"); String AGE_AS_STRING = DFormat.format(age); System.out.println("The child is " + AGE_AS_STRING + " years old"); // The child is 2 years old System.out.println(((Object)age).getClass().getSimpleName()); // Integer System.out.println(AGE_AS_STRING.getClass().getSimpleName()); // String >>
Let’s break the code down:
- To be able to use the DecimalFormat class in the example above, we imported it: import java.text.DecimalFormat; .
- We created the integer age variable.
- We then created a new object of the DecimalFormat class called DFormat .
- Using the object’s format() method, we converted age to a string: DFormat.format(age); .
Summary
In this article, we talked about converting integers to strings in Java.
We saw examples that showed how to use three different methods – Integer.toString() , String.valueOf() , String.format() — and the DecimalFormat class to convert variables from integers to strings.
Each example showed how to check the data type of a variable before and after conversion.
Преобразование целого числа в строку в Java
В этом посте мы обсудим, как преобразовать целое число в строку в Java. Если значение указанного целого числа отрицательное, решение сохранит знак в результирующей строке.
Есть много случаев, когда нам нужно преобразовать значения int, double или float в строку (и наоборот). Преобразование в строку также необходимо для предотвращения арифметического переполнения — например, 2147483648 число представляет Integer.MAX_VALUE + 1 слишком велик для хранения в типе данных int и выдает ошибку, но мы можем сохранить то же значение в объекте String, что и «2147483648» .
В Java есть много способов преобразовать целое число в строку:
1. Использование String.valueOf() метод
Класс String предоставляет valueOf() статический метод, который возвращает строковое представление указанного целочисленного аргумента.
Преобразование массива int в строку в Java
В этом посте мы обсудим, как преобразовать массив int в String в Java.
1. Использование потокового API
Вы можете использовать потоки Java 8, чтобы легко преобразовать массив int в строку. Идея состоит в том, чтобы получить IntStream для массива int и сопоставьте каждый элемент потока с объектом String. Затем выполните операцию редукции над элементами потока, используя метод Stream.reduce() метод, возвращающий Optional описывающий редуцированный объект. Это показано ниже:
