×

How to Convert Integer to String in Java

How to Convert int to String in Java

It is used when you want to convert an integer to String. You can convert int to String by using the following methods:

  • Using String.valueOf() method
  • Using Integer.toString() method
  • Using String.format() method

Using String.valueOf() method

String.valueOf() method takes an integer value as an argument and returns a String representing the int argument. This method converts int to String. The valueOf() is the static method of String class. This is a very popular method to convert int to String. The signature of the valueOf() method is given below:

public static String.valueOf(int i)

Where i is an integer that needs to be converted into a String.

Example-

In the following example, sum is a variable of type integer having value 45.  str is the variable of type String that stores string value of integer variable sum i.e. 45. valueOf(sum) is the method that contain sum as an argument.  The first println statement prints the converted value of integer i.e”45”. The second println statement “123” is concatenated to the string “45” that prints “12345”.

public class IntToStringExample
{
public static void main(String args[])
{
int sum = 45;
String str = String.valueOf(sum); //converts int to String using valueOf() method
System.out.println("String is: "+str); //printing the  sting value
System.out.println("Concatenated string is " +123+str); //concatenating the string after conversion
}
}

Output-

String is: 45
Concatenated string is 12345

Using Integer.toString() method

Integer.toString() method works same as String.valueOf() method. It belongs to the Integer class. It converts the specified integer value to String. The toString() is the static method that returns String representing the integer. A new instance of Integer class is created before conversion is performed. The signature of the toString() method is given below:

public static Integer.toString(int i)

Where i is an integer variable that requires conversion.

Example-

In the following example, var is the variable of type integer having value 22. In the sixth line, str is a variable of type String that stores the converted value of an integer variable var, i.e, “22”. Integer is a wrapper class under the java.lang package. toString(var) is the method that contains an argument which we want to convert into String, i.e. var.

The first println statement prints the value of str i.e. “22”. Next println statement prints the concatenated string i.e. “3322”. The last println statement prints the value 66 because it adds the 44 into 22.

public class IntToStringExample1
{
public static void main(String args[])
{
int var = 22;
String str = Integer.toString(var); //converts int to String using toString() method
System.out.println("String is: "+str);
System.out.println("Concatenated string is: " +33+str);
System.out.println("Adding integer value of var to 44: " +(var+44)); //adds 44 into 22
}
}

Output:

String is: 22
Concatenated string is: 3322
Adding integer value of var: 66

Using String.format() method

This method is used for formatting the String. By using this method, you can concatenate and format the output of the concatenated string at the same time. It is first introduced in JDK 5. The signature of String.format() method is given below.

public static String format(String format, Object... args)

Example-

In the following example, avg is a variable of type int having value 99. In the next line, str is a variable of type String that stores the converted value of integer variable avg. %d is the format specifier, avg is an argument. The println statement prints the “99” as a string.

public class IntToStringExample2
{ 
public static void main(String args[])
{ 
int avg = 99; 
String str = String.format("%d",avg);  //converts int to String using String.format() method
System.out.println("Average is: " +str ); 
}
}

Output:

Average is: 99

Related Topics

Jagged Array in Java

Prerequisite We must first understand what Arrays are and Multi-dimensional Arrays are before we can learn about Jagged arrays. Java Arrays: A collection of data types that are similar is called an...

5 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Program to Reverse a Number in Java

In order to reverse a number, the digit in the first place must be swapped with the digit in the final position, the second digit with the second-to-last digit, and...

3 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Array Slicing in Java

Array slicing is a method in Java for obtaining a subarray of a specified array. Assume a[] is an array. It contains eight items indexed from a[0] to a[7].a[] =...

3 minutes read.

Java String hashCode() method

Java String hashCode() method returns hash code for current String. hash code for string object is computed as s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1] Using int...

2 minutes read.

How to declare string array in Java

Introduction Array is a data structure with a fixed size, which allows us to store elements of similar type. Data of primitive types like int, char, float, string, etc. can be...

2 minutes read.

Java Boolean getBoolean() Method

The getBoolean() method of Java Boolean class returns true if the specified system property is not null and is equal to the String ‘true’, else the result returned is false. Syntax public...

1 minute read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Java Try Keyword

The try block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java try block has...

3 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

Java String Methods

Java String Methods Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java...

2 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.