int to String Java how to? Learn to convert int to String in Java? Techniques and methods such as toString, valueOf, etc convert int to string

For converting int to string java, we must understand the two data types and how to interchange them.

java int to string

int to String Conversion

Simply speaking, int data type stores numbers from –2,147,483,648 to 2,147,483,647.

Whereas, String contains a set of characters also known as char in Java. The int is a primitive data type in Java.

Unlike int, String is a pre-defined class and every word that we create using String class is treated as an Object.

Data type Size Description
byte 1 byte Stores whole number from -128 to 127
short 2 byte Stores whole number from -32,768 to 32,767
short 2 byte Stores whole number from -32,768 to 32,767
int 4 bytes Stores whole number from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional number. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional number. Sufficient for storing 15 decimal digits
boolean 1 bit Stores true or false
char 2 bytes Stores a single character/letter or ASCII values

There are a number of ways to convert an int to String in Java

Let’s take a tour.

  1. Firstly, Using toString: Integer.toString(Integer i)
  2. Second, Using concatenation: 'int i'+"";
  3. Using valueOf: String.valueOf(Integer i)
  4. Using StringBuffer/ StringBuilder: builderObject.apend(Integer i) method
  5. Finally, Using String format: String.format("%d",'int i');

Before we dive in, you should know that just like int we also have an Integer class in Java. They are both meant to store ‘numbers’ or ‘integers’ BUT int is a primitive data type whereas Integer is a wrapper class for int.

The primary advantage of Wrapper Classes is that we need Wrapper objects to function with collections which are only possible with the help of Wrapper classes. As the wrapper classes have objects we can store null as a value.

We could not store null in variables of primitive datatype.

In all these methods we have used int i to be converted to String but the same could be achieved using Integer i.

[syntax: Integer i = new Integer(1);]

toString method to change int to to String

A toString() is an in-built method in Java that returns the value given to it in string format. Hence, we can also use it to convert an int value to a String.

We call the toString method from the Integer class as shown below.

public class IntToStringDemo {
	public static void main(String[] args) {
		int i = 1;
		String s = Integer.toString(i);
		System.out.println("Java int "+i+" to String \""+s+"\"");
	}
}

OUTPUT

Java int 1 to String "1"

Concatenation technique for int to String Java conversion

This is an indirect way but a very popular one to convert int to String. I personally prefer to use it as it is the simplest among all options. We combine an empty String with the int value using the ‘+’ operator, and we get back the String value.

 public class IntToStringDemo {
	public static void main(String[] args) {
		int i = 2;
		String s = i+"";
		System.out.println("Java int "+i+" to String \""+s+"\"");
	}
}

OUTPUT

Java int 2 to String "2"

valueOf method

To use this method we use the valueOf method from the String class. While doing so we pass the Integer value.

The Java String valueOf() method converts different types of values into string. With the help of the string valueOf() method, you can convert int to string, long to string, boolean to string, character to string, float to string, double to string, object to a string, and char array to string.

public class IntToStringDemo {
	public static void main(String[] args) {
		int i = 3;
		String s = String.valueOf(i);
		System.out.println("Java int "+i+" to String \""+s+"\"");
	}
}

OUTPUT

Java int 3 to String "3"

append

The StringBuilder in Java represents a mutable sequence of characters(value can be changed). Since the String Class in Java creates an immutable sequence of characters, the StringBuilder class provides an alternative to String Class.
We create a StringBuilder object and call the append method with the int value as a parameter. The Format remains the same with StringBuffer which is thread-safe but less efficient than StringBuilder.

String Buffer String Builder
1. StringBuffer is synchronized. i.e it is thread-safe.

It means two threads can’t call the methods of StringBuffer simultaneously.

1. StringBuilder is non-synchronized i.e. it is not thread-safe.

It means two threads can call the methods of StringBuilder simultaneously.

2. StringBuffer is less efficient than StringBuilder. 2. StringBuilder is more effecient.
public class IntToStringDemo {
	public static void main(String[] args) {
		int i = 4;
		StringBuilder s = new StringBuilder();
		s.append(i);
		System.out.println("Java int "+i+" to String \""+s+"\"");
	}
}

OUTPUT

Java int 4 to String "4"

format

Use String format in the format shown. If you are from C programming background, it will seem familiar. “%d” is the format specifier for an int value. Similarly, we can use “%f” for float, “%d” for double, “%c” for char.

public class IntToStringDemo {
	public static void main(String[] args) {
		int i = 5;
		String s = String.format("%d", i);
		System.out.println("Java int "+i+" to String \""+s+"\"");
	}
}

OUTPUT

Java int 5 to String "5"

int to String Java | Programming Example

It is a little unusual to change numbers into words. But think of a scenario where we are given a number and we will have to format the number with a dollar ‘$’ sign and commas. These characters are not numbers.

So how do we do it?

Well, we shall convert The int value to a String type using Integer.toString('Integer value') the method. As the name suggests we pass an Integer value to the method and the method returns a String. Here’s the code for you.

public class IntToString {
	public static void main(String[] args) {
		int amount = 10000;
		String dollar_amount = Integer.toString(amount);
		System.out.println("amount: $"+dollar_amount);
	}
}

OUTPUT

amount: $10000

It is good that we can print the dollar sign with the amount but where are the commas?

Well, for them we have to iterate through each digit of the number and add commas in regular intervals of 3 digits from the end. If you have a good grasp of loops, be confident and go ahead try it on your own.

[Hint: create a new String and use charAt('int index') method to get each digit of the previous String.

Great! if you tried. Here is the solution for help.

public class IntToString {
	public static void main(String[] args) {
		int amount = 100000;
		String dollar_amount = Integer.toString(amount);
		String new_dollar_amount = "";
		int count_digit = 0;
		/*traverse the string in reverse order*/
		for(int i= dollar_amount.length()-1; i>=0; i--) {
			count_digit++;
			
			/* add commas after 3 digits*/
			if(count_digit % 4 == 0)
				new_dollar_amount += ',';
			
			new_dollar_amount = new_dollar_amount + dollar_amount.charAt(i);
			/*Alternate syntax:
			 * new_dollar_amount += dollar_amount.charAt(i);
			 */
			
		}
		System.out.println("amount: $"+new_dollar_amount);
	}
}

OUTPUT

amount: $000,001

Wait! that is not correct! Our amount value is completely reversed.
We can fix that using the common solution for reversing a String or we can use the pre-defined method of the StringBuilder class in Java to get it done in one line!

public class IntToString {
	public static void main(String[] args) {
		int amount = 100000;
		String dollar_amount = Integer.toString(amount);
		String new_dollar_amount = "";
		int count_digit = 0;
		/*traverse the string in reverse order*/
		for(int i= dollar_amount.length()-1; i>=0; i--) {
			count_digit++;
			
			/* add commas after 3 digits*/
			if(count_digit % 4 == 0)
				new_dollar_amount += ',';
			
			new_dollar_amount = new_dollar_amount + dollar_amount.charAt(i);
			/*Alternate syntax:
			 * new_dollar_amount += dollar_amount.charAt(i);
			 */
			
		}
		/*code to reverse value of new_dollar_amount starts*/
		StringBuilder fixed_amount = new StringBuilder(new_dollar_amount);
		fixed_amount.reverse();
		/*code to reverse value of new_dollar_amount ends*/
		System.out.println("amount: $"+fixed_amount);
	}
}

OUTPUT

amount: $100,000

You can also read out popular posts on How to Compare Two Strings?

We hope this tutorial helped you to achieve the same. Keep learning keep sharing. Follow us on Facebook and Instagram.