×

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

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

Java Command Line Arguments

Java command line arguments Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and...

2 minutes read.

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.

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

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 Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Example of Static import in Java

Static keyword Java's static keyword is mainly used to control memory. Variables, methods, blocks, and nested classes are compatible with the static keyword. Instead of an instance, the static keyword is...

3 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

Java If Keyword

Definition: The if statement specifies a section of Java code that will run if an if statement's condition is false. The following conditional statements can be used in Java: To provide a block...

3 minutes read.