Typecasting : How to Convert ‘int’ to ‘String’ in JAVA

Typecasting, or type conversion, is a method of changing an entity from one data type to another. It is used in computer programming to ensure variables are correctly processed by a function. An example of typecasting is converting an integer to a string.

It is important to ensure that the casting is in line with run-time rules as well as compile-time rules in Java. Reference type casting is further divided into two types:

Upcasting

Upcasing involves the conversion of an object of SubType into an object of SuperType. Java has the provision for assigning the object without requiring the addition of an explicit cast. The compiler would know what is being done and would cast the SubType value to SuperType. This way, the object is brought to a basic level. Developers can add an explicit cast without worrying about any issues.

Downcasting

Downcasting involves the conversion of a SuperType object into a Subtype object. It is the most frequently used casting wherein it is communicated to the compiler that the value of the base object isn’t it’s own but of the SuperType object.

String.valueOf()

The String.valueOf() method converts int to String. The valueOf() is the static method of String class. The signature of valueOf() method is given below:

public static String valueOf(int i)
Java int to String Example using String.valueOf()
Let’s see the simple code to convert int to String in java.

int i=10;
String s=String.valueOf(i);//Now it will return “10”
Let’s see the simple example of converting String to int in java.

public class IntToStringExample1{
public static void main(String args[]){
int i=200;
String s=String.valueOf(i);
System.out.println(i+100);//300 because + is binary plus operator
System.out.println(s+100);//200100 because + is string concatenation operator
}}

Integer.toString()

The Integer.toString() method converts int to String. The toString() is the static method of Integer class. The signature of toString() method is given below:

public static String toString(int i)
Java int to String Example using Integer.toString()
Let’s see the simple code to convert int to String in java using Integer.toString() method.

int i=10;
String s=Integer.toString(i);//Now it will return “10”
Let’s see the simple example of converting String to int in java.

public class IntToStringExample2{
public static void main(String args[]){
int i=200;
String s=Integer.toString(i);
System.out.println(i+100);//300 because + is binary plus operator
System.out.println(s+100);//200100 because + is string concatenation operator
}}

String.format()

The String.format() method is used to format given arguments into String. It is introduced since Jdk 1.5.

public static String format(String format, Object… args)
Java int to String Example using String. format()
Let’s see the simple code to convert int to String in java using String. format() method.

public class IntToStringExample3{
public static void main(String args[]){
int i=200;
String s=String.format("%d",i);
System.out.println(s);
}}

Happy Coding


Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top