- How to convert string to int or integer to string in java?
- When we are working on a project we will be having some scenarios of expecting a string from int or integer from string.
- Now we will be discussing on how to convert Integer to String in java with example java programs.
1.Convert Integer to String using Integer.toString() method:
2.Convert Integer to String using String.valueOf() method.
3.Convert Integer to String / int to String using new Integer(int).toString()
4.Convert Integer to String / int to String using String.format() method
5.Convert Integer to String / int to String using DecimalFormat
6.Convert Integer to String/ int to String using StringBuffer / StringBuilder
7.Convert Integer to String / int to String directly by adding to ""
8.Convert Integer to String / int to String using Special radix.
1.Convert Integer to String using Integer.toString() method:
- Integer is a wrapper class in java which is mainly used to represent or convert primitive int value to object.
- And Integer class having some predefined methods.
- Integer class has special predefined method to convert integer value to string toString();
- toString is a static method in Integer class so that by using class name itself we can call that method to convert Integer to corresponding string value.
Output:
- package convertintegertostring.java;
- public class IntegerToString {
- /**
- * @ website: www.InstanceOfJava.com
- */
- public static void main(String[] args) {
- Integer i = new Integer(64);
- //converting integer to string by calling toString() method on integer object
- String str= i.toString();
- // also we can use like String str= Integer.toString(i);
- System.out.println(str);
- }
- }
- 64
2.Convert Integer to String using String.valueOf() method.
- Integer class have toString() method to convert integer to string value same way String class also has valueOf() method to convert int or integer to string.
- String class has a static method valueOf() which takes int as argument and converts into string.
Program #2: Java Example program to convert int to string using valueOf() method.
- package convertintegertostring.java;
- public class IntegerToString {
- /**
- * @ website: www.InstanceOfJava.com
- */
- public static void main(String[] args) {
- Integer i = new Integer(64);
- //converting integer to string by calling valueOf() method on String clas
- String str= String.valueOf(i);
- System.out.println(str);
- int number=123;
- String numberstr= String.valueOf(number);
- System.out.println(numberstr);
- }
- }
- 64
- 123
3.Convert Integer / int to String using new Integer(int).toString()
- And here by creating Integer object and directly calling tostring method.
- We can convert into to string directly with one statement this will be done by using Integer(int).toString()
Program #3: Java Example program to convert int to string using new Integer(int ).toString()
- package convertintegertostring.java;
- public class IntegerToString {
- /**
- * @ website: www.InstanceOfJava.com
- */
- public static void main(String[] args) {
- int number=23;
- String numberstr= new Integer(number).toString();
- System.out.println(numberstr);
- }
- }
- 23
4.Convert Integer / int to String using String.format() method
- Another alternative way to convert int t string is by calling String.format method.
- String.format ("%d", number);
Program #4: Java Example program to convert int to string using String.format() method
- package convertintegertostring.java;
- public class IntegerToString {
- /**
- * @ website: www.InstanceOfJava.com
- */
- public static void main(String[] args) {
- int number=23;
- String numberstr= String.format ("%d", number);
- System.out.println(numberstr);
- }
- }
- 23
5.Convert Integer / int to String using DecimalFormat
- Another alternative way to convert int t string is by java.text.DecimalFormat
- DecimalFormat class mainly used to represent numbers in formats.
- Decimal format class providing format() method to convert int to string with specified format.
Program #5: Java Example program to convert int to string using DecimalFormat class
- package convertintegertostring.java;
- public class IntegerToString {
- /**
- * @ website: www.InstanceOfJava.com
- */
- public static void main(String[] args) {
- int number=23;
- DecimalFormat obj= new DecimalFormat("#");
- String numberstr= obj.format(number);
- System.out.println(numberstr);
- DecimalFormat objct= new DecimalFormat("#,###");
- String numberstr1= objct.format(3400);
- System.out.println(numberstr1);
- }
- }
- 23
- 3,400
6.Convert Integer / int to String using StringBuffer / StringBuilder
- StringBuffer and StringBuilder classes also providing toString() method to convert int to string
Program #6: Java Example program to convert int to string using StringBuffer and StringBuilder
- package convertintegertostring.java;
- public class IntegerToString {
- /**
- * @ website: www.InstanceOfJava.com
- */
- public static void main(String[] args) {
- int number=234;
- StringBuffer sf=new StringBuffer();
- sf.append(number);
- String numberstr= sf.toString();
- System.out.println(numberstr);
- StringBuilder sb=new StringBuilder();
- sb.append(number);
- String nstr= sb.toString();
- System.out.println(nstr);
- }
- }
- 234
- 234
7.Convert Integer / int to String directly by adding to ""
- We can convert a number to string simply by concatenating to "".
- But it is not recommended to use.
Program #7: Java Example program to convert int to string by concat to ""
- package convertintegertostring.java;
- public class IntegerToString {
- /**
- * @ website: www.InstanceOfJava.com
- */
- public static void main(String[] args) {
- int number=8538;
- String numberstr= ""+number;
- System.out.println(numberstr);
- }
- }
- 8538
8.Convert Integer / int to String using Special radix.
Program #8: Java Example program to convert int to string using special radix
- package convertintegertostring.java;
- public class IntegerToString {
- /**
- * @ website: www.InstanceOfJava.com
- */
- public static void main(String[] args) {
- int number=536;
- String binaryString = Integer.toBinaryString(number);
- System.out.println(binaryString);
- String octalString = Integer.toOctalString(number);
- System.out.println(octalString);
- String hexString = Integer.toHexString(number);
- System.out.println(hexString);
- String customString = Integer.toString(number, 7);
- System.out.println(customString);
- }
- }
- 1000011000
- 1030
- 218
- 1364
8 different ways to convert int to string in java
Output:
- 64
- 64
- 536
- 536
- 3,400
- 536
- 536
- 1000011000
No comments