Java – Integer to String conversion in 6 ways

In this article, we will discuss various ways to convert int to String in Java

Q) What is the need of converting primitive int or wrapper Integer type to String ?

  • Generally, whenever we receive or send data from web application then it all passed in the form of String only
  • After mathematical calculation, all numbers need to be converted into String for displaying
  • This article explains about primitive int or Integer wrapper-type to String conversion only, but we can do conversion for other types like long, double, float, etc. to String object
  • Note: Likewise, sometimes String to Integer conversion is also required

1. Various ways to convert int or Integer to String

  1. using Interger.toString(intValue);
  2. using String.valueOf(intValue);
  3. using String.format(type, number);
  4. create Integer object and then convert to String using toString() method
  5. adding double quotes (“”) to int value, at the beginning
  6. using append() method of StringBuffer & StringBuilder

Read String class in detail with example

Let us move forward and discuss all possible ways to convert Integer to String in Java

1.1 Using Interger.toString(intValue) method

  • This method can be used to convert primitive int data-type or Integer wrapper-type to String object
  • Negative integer data-type can also be converted to String type, as shown in the below example

Method signature:

public static String toString(int i);

ConvertIntegerIntoStringUsingToStringMethod.java

package in.bench.resources.integer.to.string.conversion;

public class ConvertIntegerIntoStringUsingToStringMethod {

	public static void main(String[] args) {

		// primitive int data-type
		int iValue1 = 54545;

		// 1. converting int to String
		// by using toString() method
		String str1 = Integer.toString(iValue1);
		System.out.println("1. Converted primitive"
				+ " int to String value is : " + str1);

		// primitive int data-type (negative value)
		int iValue2 = -54545;

		// 2. converting int to String
		// by using toString() method
		String str2 = Integer.toString(iValue2);
		System.out.println("\n2. Converted negative primitive"
				+ " int to String value is : " + str2);

		// Integer object
		Integer iValue3 = new Integer(12345);

		// 3. converting Integer object to String
		// by using toString() method
		String str3 = Integer.toString(iValue3);
		System.out.println("\n3. Converted"
				+ " Integer object to String value is : " + str3);
	}
}

Output:

1. Converted primitive int to String value is : 54545

2. Converted negative primitive int to String value is : -54545

3. Converted Integer object to String value is : 12345

1.2 Using String.valueOf(intValue) method

  • This method can be used to convert primitive int data-type or Integer wrapper-type to String object
  • Negative integer data-type can also be converted to String type, as shown in the below example

Method signature:

public static String valueOf(int i);

ConvertIntegerIntoStringUsingValueOfMethod.java

package in.bench.resources.integer.to.string.conversion;

public class ConvertIntegerIntoStringUsingValueOfMethod {

	public static void main(String[] args) {

		// primitive int data-type
		int iValue1 = 98989;

		// 1. converting int to String
		// by using valueOf() method
		String str1 = String.valueOf(iValue1);
		System.out.println("1. Converted primitive"
				+ " int to String value is : " + str1);

		// primitive int data-type (negative value)
		int iValue2 = -98989;

		// 2. converting negative int to String
		// by using valueOf() method
		String str2 = String.valueOf(iValue2);
		System.out.println("\n2. Converted negative primitive"
				+ " int to String value is : " + str2);

		// Integer object
		Integer iValue3 = new Integer(12345);

		// 3. converting Integer object to String
		// by using valueOf() method
		String str3 = String.valueOf(iValue3);
		System.out.println("\n3. Converted"
				+ " Integer object to String value is : " + str3);
	}
}

Output:

1. Converted primitive int to String value is : 98989

2. Converted negative primitive int to String value is : -98989

3. Converted Integer object to String value is : 12345

1.3 Using String.format(type, number); method

  • This method can be used to convert primitive int data-type or Integer wrapper-type to String object
  • Negative integer data-type can also be converted to String type, as shown in the below example

Method signature:

public static String format(String format, Object... args);

ConvertIntegerIntoStringUsingFormatMethod.java

package in.bench.resources.integer.to.string.conversion;

public class ConvertIntegerIntoStringUsingFormatMethod {

	public static void main(String[] args) {

		// primitive int data-type
		int iValue1 = 66442;

		// 1. converting int to String
		// by using format() method
		String str1 = String.format("%d", iValue1);
		System.out.println("1. Converted primitive"
				+ " int to String value is : " + str1);

		// primitive int data-type (negative value)
		int iValue2 = -66442;

		// 2. converting negative int to String
		// by using format() method
		String str2 = String.format("%d", iValue2);
		System.out.println("\n2. Converted negative primitive"
				+ " int to String value is : " + str2);

		// Integer object
		Integer iValue3 = new Integer(54321);

		// 3. converting Integer object to String
		// by using format() method
		String str3 = String.format("%d", iValue3);
		System.out.println("\n3. Converted"
				+ " Integer object to String value is : " + str3);
	}
}

Output:

1. Converted primitive int to String value is : 66442

2. Converted negative primitive int to String value is : -66442

3. Converted Integer object to String value is : 54321

1.4 Create Integer object and then invoke toString() method {new Integer(intValue).toString();}

  • Here, we will create new Integer object with primitive int value as constructor-argument
  • After creating new Integer object by passing primitive int value, then invoke toString() method for converting primitive int data-type to String object

Method signature:

public Integer(int iValue);

public String toString();

ConvertIntegerIntoStringUsingObjectCreation.java

package in.bench.resources.integer.to.string.conversion;

public class ConvertIntegerIntoStringUsingObjectCreation {

	public static void main(String[] args) {

		// primitive int data-type
		int iValue1 = 875421;

		// 1. converting int to String
		// by creating Integer object
		Integer integerObj1 = new Integer(iValue1);

		// and then invoke toString(); method
		String str1 = integerObj1.toString();
		System.out.println("1. Converted primitive"
				+ " int to String value is : " + str1);

		// primitive int data-type (negative value)
		int iValue2 = -875421;

		// 2. converting negative int to String
		// by creating Integer object
		Integer integerObj2 = new Integer(iValue2);

		// and then invoke toString(); method
		String str2 = integerObj2.toString();
		System.out.println("\n2. Converted negative primitive"
				+ " int to String value is : " + str2);

		// Integer object
		Integer iValue3 = new Integer(14785);

		// 3. converting Integer object to String
		// by creating Integer object
		String str3 = iValue3.toString();
		System.out.println("\n3. Converted"
				+ " Integer object to String value is : " + str3);
	}
}

Output:

1. Converted primitive int to String value is : 875421

2. Converted negative primitive int to String value is : -875421

3. Converted Integer object to String value is : 14785

1.5 Adding double quotes (“”) to int value

  • Note: for this conversion to work, double-quotes (“”) must be added at the start of the concatenation

Syntax:

String temp = “” + inValue;

ConvertIntegerIntoStringByAddingDoubleQuotes.java

package in.bench.resources.integer.to.string.conversion;

public class ConvertIntegerIntoStringByAddingDoubleQuotes {

	public static void main(String[] args) {

		// primitive int data-type
		int iValue1 = 985632;

		// 1. converting int to String
		// by adding ""
		String str1 = "" + iValue1;
		System.out.println("1. Converted primitive"
				+ " int to String value is : " + str1);

		// primitive int data-type (negative value)
		int iValue2 = -985632;

		// 2. converting negative int to String
		// by adding ""
		String str2 = "" + iValue2;
		System.out.println("\n2. Converted negative primitive"
				+ " int to String value is : " + str2);

		// Integer object
		Integer iValue3 = new Integer(54321);

		// 3. converting Integer object to String
		// by adding ""
		String str3 = "" + iValue3;
		System.out.println("\n3. Converted"
				+ " Integer object to String value is : " + str3);
	}
}

Output:

1. Converted primitive int to String value is : 985632

2. Converted negative primitive int to String value is : -985632

3. Converted Integer object to String value is : 54321

1.6 Using append() method of StringBuffer & StringBuilder

  • This method can be used to convert primitive int data-type to String object
  • Only sign is allowed, like minus (-)

Syntax:

StringBuffer sb = sb.append(intValue);

String temp = sb.toString();

ConvertIntegerIntoStringByAppending.java

package in.bench.resources.integer.to.string.conversion;

public class ConvertIntegerIntoStringByAppending {

	public static void main(String[] args) {

		// primitive int data-type
		int iValue1 = 213659;

		// 1. converting int to String
		// by using append() method
		StringBuffer sb1 = new StringBuffer();
		String str1 = sb1.append(iValue1).toString();
		System.out.println("1. Converted primitive"
				+ " int to String value is : " + str1);

		// primitive int data-type (negative value)
		int iValue2 = -213659;

		// 2. converting negative int to String
		// by using append() method
		StringBuffer sb2 = new StringBuffer();
		String str2 = sb2.append(iValue2).toString();
		System.out.println("\n2. Converted negative primitive"
				+ " int to String value is : " + str2);

		// Integer object
		Integer iValue3 = new Integer(867514);

		// 3. converting Integer object to String
		// by using append() method
		StringBuffer sb3 = new StringBuffer();
		String str3 = sb3.append(iValue3).toString();
		System.out.println("\n3. Converted"
				+ " Integer object to String value is : " + str3);
	}
}

Output:

1. Converted primitive int to String value is : 213659

2. Converted negative primitive int to String value is : -213659

3. Converted Integer object to String value is : 867514

Note: Likewise, StringBuilder can also be used and main difference between them is synchronization in a multi-threaded environment

2. Auto-boxing and un-boxing feature from Java 1.5 version:

  • parseInt() method returns primitive int data-type, but it can be easily used as Integer wrapper-type
  • as auto-boxing feature helps to convert primitive data-type to wrapper types
  • similarly, Integer.valueOf() method returns Integer wrapper type, but it can also be used as primitive int
  • let’s see one example on this auto-boxing feature

Q) What if we want to convert primitive int to Integer wrapper-type or vice-versa ?

  • Auto-boxing feature available from Java 1.5 version
  • So, converting primitive data-type to wrapper-type can easily be done, by directly assigning
  • Let’s see one example based on this auto-boxing feature

AutoBoxingFeatureForConversion.java

package in.bench.resources.string.to.integer.conversion;

public class AutoBoxingFeatureForConversion {

	public static void main(String[] args) {

		// String with only digits
		String str1 = "12345";

		// string to int conversion
		Integer iValue1 = Integer.parseInt(str1);

		// 1. Auto-Boxing - converting Integer to int
		int iAutoBoxing = iValue1;
		System.out.println("1. Auto-Boxing : " + iAutoBoxing);

		// String with only digits and a minus sign (-)
		String str2 = "-98765";

		// string to int conversion
		int iValue2 = Integer.valueOf(str2);

		// 2. Auto-Boxing - converting String to int
		int iUnBoxing = iValue2;
		System.out.println("\n2. Un-Boxing   : " + iUnBoxing);
	}
}

Output:

1. Auto-Boxing : 12345

2. Un-Boxing   : -98765

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String to float conversion in 3 ways
Java - String to int conversion in 3 ways