Wednesday, March 22, 2017

How do you convert integer to String in Java?

Different possible ways to convert integer into String!
The first one is the easiest and the simplest:
int i = 1234;
String s = "" + i;
The second method uses the valueOf()function of the String class:
int i = 1234;
String s = String.valueOf(i);
The third one requires wrapper class Integer and uses its toString() function:
int i = 1234;
String s = new Integer(i).toString();
One more program
public class Test { public static void main(String[] args) {
// First Method
System.out.println(String.valueOf(12345));
// Second Method
System.out.println(Integer.toString(12345));
// Third Method
System.out.println(new Integer(12345).toString());
// Fourth Method
System.out.println(String.format("%d", 12345));
// Fifth Method
System.out.println(new StringBuffer().append(12345).toString());
// Six Method
System.out.println(new StringBuilder().append(12345).toString()); // Seventh Method
int num = 12345; String numString = "" + num; System.out.println(numString);
}
}
Output
12345
12345
12345
12345
12345
12345
12345


2 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  2. Hello Guys don't delay Mumbai trip because Taaj Mumbai escorts girls agency wait for you, write-my-essay.cheap it's a great dating hub, so you can enjoy with us according your date and time.

    ReplyDelete