How to convert int to String in java


In this core java tutorial we will learn how to convert int primitive type to String in java and convert Integer object to String with program and examples in java. We will  also understand  When to use convert String to int primitive type and when when to convert String to int object in java.





Programs to convert int to String in java -
  • Program/Example 1 to How to convert int primitive type to String  in java.
  • Program/Example 2 to How to convert Integer object to String in java


Program/Example 1 to How to convert int primitive type to String  in java.

String.valueOf(int) method converts int primitive type to String  in java.

package convertIntToStringPkg;
public class ConvertIntToStringExample1 {
   public static void main(String[] args) {
          int i = 12;
          String string = String.valueOf(i);
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12

*/



Program/Example 2 to How to convert Integer object to String in java

toString() method of java.lang.Integer class convert int object to String in java.

package convertIntToStringPkg;
public class ConvertIntObjectToStringExample2 {
   public static void main(String[] args) {
          Integer integerObject = 12;
          String string = integerObject.toString();
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12
*/


Summary -

In this core java tutorial we learned conversion of int primitive type to String in java and convert Integer object to String with program and examples in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

Collection - List, Set and Map all properties in tabular form



No comments:

Post a Comment