Table of contents
1.
Introduction
2.
How to Convert int to String in Java?
3.
String.valueOf()
3.1.
Syntax
3.2.
Java int to String Example using String.valueOf()
3.3.
Java
4.
Integer.toString()
4.1.
Syntax
4.2.
Java int to String Example using Integer.toString()
4.3.
Java
5.
String.format()
5.1.
Syntax
5.2.
Java int to String Example using String.Format()
5.3.
Java
6.
Frequently Asked Questions
6.1.
How many primitive data types are present in Java?
6.2.
What are the three most commonly used methods for int to String conversion?
6.3.
Why do we need to convert an int to a String?
6.4.
What are primitive data types?
7.
Conclusion
Last Updated: Sep 8, 2024
Medium

Int to String Conversion in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is an object-oriented, class-based, high-level programming language. It is based on the concept of WORA (write once use anywhere) for developer ease. Java classes are compiled into byte code. The compiled byte codes can be used on any platform supporting Java without recompiling the class.

”int to string jav

We have eight primitive or native data types in Java, namely,

  • int
  • short
  • long
  • byte
  • float
  • double
  • boolean
  • char

We also have the option of multiple non-primitive data types in Java. The String is one of Java's most commonly used non-primitive data types. And there often arises the need to convert int to String in Java.

How to Convert int to String in Java?

Using String.valueOf() method:

  • Converts an int to a String by returning the string representation of the integer.
  • Example: String str = String.valueOf(123);

Using Integer.toString() method:

  • Converts an integer to a string using the toString() method of the Integer class.
  • Example: String str = Integer.toString(123);

Using string concatenation (+):

  • Converts an int to a String by concatenating it with an empty string.
  • Example: String str = 123 + "";

Using String.format():

  • Converts an integer to a string using String.format().
  • Example: String str = String.format("%d", 123);

There are multiple functions used for the conversion of int to String in Java, for instance,

  • String.valueOf()
  • int. toString()
  • String.format()

String.valueOf()

The String.valueOf function is the most commonly used function for the conversion of int to String in Java. The valueOf method belongs to the String class and is a static method. Let us look at the syntax for the same,

Syntax

String s=String.valueOf(i);

Java int to String Example using String.valueOf()

  • Java

Java

The complete code will look like this,
class int_to_string{
public static void main(String [] args){
int number = 50;
String string_number = String.valueOf(number);
System.out.println("This is the Integer value + 100: "+ (number+100));
System.out.println("This is the String value + 100: "+ string_number+100);
}
}
You can also try this code with Online Java Compiler
Run Code

 

In the above code, we have the integer to string in Java conversion using String.valueOf() function. 100 gets added to the integer value, whereas it gets concatenated with the string value, thus producing different results and depicting the conversion from int to string in Java.

Also see, Duck Number in Java

Integer.toString()

The Integer.toString function is also a widely used function for converting int to String in Java. The toString method belongs to the String class and is a static method. Let us look at the syntax for the same,

Syntax

String s = Integer.toString(i);

Java int to String Example using Integer.toString()

  • Java

Java


class int_to_string{
public static void main(String [] args){
int number = 50;
String string_number = Integer.toString(number);
System.out.println("This is the Integer value + 100: "+ (number+100));
System.out.println("This is the String value + 100: "+ string_number+100);
}
}
You can also try this code with Online Java Compiler
Run Code

In the above code, we have the integer to string in Java conversion using Integer.toString() function.  100 gets added to the integer value, whereas it gets concatenated with the string value, thus producing different results and depicting the conversion from int to string in Java.

String.format()

The String.format method is used to format a given Data type to the String Data type using its String format. Let us look at an example for conversion of int to string in Java using String.format(),

Syntax

class int_to_string{
You can also try this code with Online Java Compiler
Run Code

Java int to String Example using String.Format()

  • Java

Java

public static void main(String [] args){
int number = 50;
String string_number = String.format("%d",number);
System.out.println("This is the Integer value + 100: "+ (number+100));
System.out.println("This is the String value + 100: "+ string_number+100);
}
}
You can also try this code with Online Java Compiler
Run Code

In the above code, we have the integer to string in Java conversion using String.format() function.  100 gets added to the integer value, whereas it gets concatenated with the string value, thus producing different results and depicting the conversion from int to string in Java.

Try it by yourself on Java Online Compiler.

Also check out Addition of Two Numbers in Java here.

Frequently Asked Questions

How many primitive data types are present in Java?

We have eight primitive or native data types in Java: int, short, long, byte, float, double, boolean, and char.
 

What are the three most commonly used methods for int to String conversion?

The three most commonly used methods in Java for the conversion of int to String are: String.valueOf(), “data_type”. toString(), String.format().
 

Why do we need to convert an int to a String?

The need for int to String conversion arises when the developers are provided with such huge numbers that they cannot be stored in the primitive Data types in Java.
 

What are primitive data types?

In computer programming, Primitive Data types are the set of variable types from which all the other data types are derived.

Conclusion

This blog explained the ways to convert an int to a String in Java, along with examples to help understand how to implement them.

Check out this problem - Multiply Strings

Recommended Readings:


Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass