Shirley Ideas All articles

Int to String in Java: One-Liner Conversion Techniques

By Shirley Sep 05, 2026
Java string methods
Java string methods

Mastering data manipulation in Java often involves converting numbers to strings and vice versa. One common scenario is converting an integer to a string. Here's a simple yet powerful guide on how to achieve this in Java, along with some useful tips and tricks.

Java string methods
Java string methods

The most straightforward way to convert an int to a string in Java is by using the Integer.toString() method or by calling the String() constructor. Let's explore both these approaches.

Java Strings Explained | String Basics, Methods & Immutability | Day 19
Java Strings Explained | String Basics, Methods & Immutability | Day 19

Approach 1: Using Integer.toString()

The Integer.toString() method is a static method that takes an int as an argument and returns a String representation of the integer.

Java String Methods
Java String Methods

Syntax: Integer.toString(int i)

Basic Usage

Java Strings 🔤 | Complete String Methods Cheat Sheet
Java Strings 🔤 | Complete String Methods Cheat Sheet

Here's a simple example illustrating its use:


int num = 123;
String strNum = Integer.toString(num);
System.out.println(strNum); // Prints: 123

String Formatting

This method also allows for formatting the output string. You can specify a radix (base) for the number system, such as hexadecimal (base 16) or binary (base 2).

Java Programming Tutorial - - toString
Java Programming Tutorial - - toString


int num = 123;
String hexNum = Integer.toString(num, 16);
String binNum = Integer.toString(num, 2);
System.out.println(hexNum); // Prints: 7b
System.out.println(binNum); // Prints: 1111011

Approach 2: Calling the String() Constructor

Another way to convert an int to a string in Java is by calling the String() constructor and passing the int as an argument.

Syntax: new String(int i)

Java String to Int
Java String to Int

Basic Usage

Here's an example showing how to use the String() constructor to convert an int to a string:

JAVA STRING METHOD
JAVA STRING METHOD
the different types of strings that are used to play in various styles and colors, with text
the different types of strings that are used to play in various styles and colors, with text
Java Strings Cheat Sheet  | Complete String Methods Guide for Beginners
Java Strings Cheat Sheet | Complete String Methods Guide for Beginners
Java String vs StringBuilder | Mutability, Performance & Key Differences | Day 21
Java String vs StringBuilder | Mutability, Performance & Key Differences | Day 21
iterating through elements in a string | Java
iterating through elements in a string | Java
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
a poster with different types of strings on it's back and front pages, including the
a poster with different types of strings on it's back and front pages, including the
Javascript String Method
Javascript String Method


int num = 123;
String strNum = new String(num);
System.out.println(strNum); // Prints: 123

Strings and Internationalization

The String() constructor also allows for better integration with internationalization (I18N) as it uses the default locale of the thread to convert the integer.

To take full advantage of this, you would typically use the NumberFormat class, like so:


import java.util.NumberFormat;

...

int num = 123;
NumberFormat formatter = NumberFormat.getInstance();
String strNum = formatter.format(num);
System.out.println(strNum); // Prints: 123 (or localized equivalent)

While both approaches work well for converting an int to a string in Java, the choice between them often depends on your specific needs, such as whether you're working with internationalized strings or require special formatting.

In conclusion, while Integer.toString() and the String() constructor are both effective for converting integers to strings in Java, the best approach ultimately depends on your specific use case. Keep these methods in your toolkit and use them to streamline your coding and enhance your programs' functionality.