Int to String in java | Integer to String in java
Being a programmer you will be in a situation where data type conversion is required. So In this article we will look into the must important type conversion that is required maximum time.
1. How to convert int to String in java ?
There are two ways to convert int data type into String as
Approach 1 :
String class has static method valueOf(int i), This method in overloaded for all the primitive data types
Prototype of this method is as :
public static String valueOf(int i)
Sample code :
int number = 10;
String num = String.valueOf(number);
Approach 2:
Integer class has static method toString(int i), This method can also be used to convert int into String
Prototype of this method is as :
public static String toString(int i)
Sample code :
int number = 10;
String num = Integer.toString(rupees);
Note : In Approach 1, String class valueOf(int i) method internally use Integer class toString(int i) method. So we can say approach one internally use approach two. You can use anyone of them to convert int into String.
2. How to convert Integer to String in java ?
There are two ways to convert Integer data type into String as
Approach 1 :
String class has static method valueOf(int i), This method in overloaded for all the primitive data types
Prototype of this method is as :
public static String valueOf(int i)
Sample code :
Integer number = 10;
String num = String.valueOf(number);
Approach 2:
Integer class has static method toString(int i), This method can also be used to convert int into String
Prototype of this method is as :
public static String toString(int i)
Sample code :
Integer number = 10;
String num = Integer.toString(rupees);
Note : In Approach 1, String class valueOf(int i) method internally use Integer class toString(int i) method. So we can say approach one internally use approach two. You can use anyone of them to convert int into String.
No comments:
Post a Comment