In this post, I am going to explain different ways to convert an Integer object to String in Java.
Approach 1: Using toString() method.
String str1 = i1.toString();
Approach 2: Using String.valueOf method.
String str2 = String.valueOf(i1);
Approach 3: Concatenate int value to an empty string.
String str3 = i1.intValue()+"";
Approach 4: Using StringBuilder.
String str4 = new StringBuilder().append(i1).toString();
Approach 5: Using StringBuffer.
String str5 = new StringBuffer().append(i1).toString();
Find the below working application.
IntegerToStringDemo.java
package com.sample.app.strings;
public class IntegerToStringDemo {
public static void main(String[] args) {
Integer i1 = 234;
String str1 = i1.toString();
String str2 = String.valueOf(i1);
String str3 = i1.intValue() + "";
String str4 = new StringBuilder().append(i1).toString();
String str5 = new StringBuffer().append(i1).toString();
System.out.println("str1 : " + str1);
System.out.println("str2 : " + str2);
System.out.println("str3 : " + str3);
System.out.println("str4 : " + str4);
System.out.println("str5 : " + str5);
}
}
Output
str1 : 234 str2 : 234 str3 : 234 str4 : 234 str5 : 234
No comments:
Post a Comment