Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
String.valueOf()
3.
Integer.toString()
4.
String.format()
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Int to String Conversion in Java

Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

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.

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.

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,

String s=String.valueOf(i);
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);
}
}

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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,

String s = Integer.toString(i);
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);
}
}

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(),

class int_to_string{
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);
}
}

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.

FAQs

  1. What is Java?
    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.
     
  2.  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.
     
  3. 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().
     
  4. 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.
     
  5. 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.

Key Takeaways

This Blog covered all the necessary points about all the possible ways to convert an int Data Type to the String Data type in Java. We further looked at examples to understand the implementation.

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