If you have an int variable and wants to convert that into String object e.g. 1 to "1" or 10 to "10" then you have come to right place. In the past I have show explained Enum to String, how to convert String to float, String to double, String to int , and String to boolean and in this article, I will show you how to convert an int to String in Java. And, not one or two but I will show you 4 different ways to convert an int to String in Java, from easy to difficult and from common to unknown. Even though it's good to know multiple ways to solve a problem e.g. to perform this conversion, I will also suggest you what is the best way to convert int to String and why?
5 Examples to convert int to String in Java
Here is the 5 ways to convert an int value to String object in Java :
1. String.concat()
2. Using + operator
3. String.valueOf()
4. Integer.toString()
5 String.format()
1. String concatenation using + operator in Java
Easiest way to convert an int primitive value to String is by just concatenating int to an empty String e.g.
String fromInt = "" + value;
Internally this code gets converted into StringBuilder e.g. new StringBuilder().append("").append(vlaue).toString(); so it does unnecessarily allocate a larger buffer (default size of StringBuilder is 16) then required but its very easy at the face, and good for testing, demo purpose.
2. int to String using concat() method
You can also use concat() method instead of + operator for doing String concatenation in Java.
3. String.valueOf() to Convert int to String in Java
In Java, the standard way to convert anything to String is by using String.valueOf() method. So you can also convert an int to String using this method as shown below :
int value = 20;
String str = String.valueOf(value);
The String returned by this method is exactly same as the one returned by Integer.toString() method.
4. Integer.toString() Example
This is not the dirct but another way to convert int to String in Java. You shouldn't be using it unless you are converting an Integer to String rather than an int to String, but its worth knowing. You can first use autoboxing to wrap an int to Integer and then call its toString() method as shown below :
Integer i = 10;
String s = i.toString();
The String returned is exactly same as that returned by Integer.valueOf() method.
5. String.format() Example
This is a rather un-common way to convert an int to String, works only after Java 1.5 because format() method was added on that release. Here is how you can use format() method to convert an int to String in Java :
String quantity = String.format("%d", 10000);
Even though main purpose of String.format() method is to produce formatted String, you can also use it like this to get String from an int variable.
And, here is a summary of all the 5 ways to convert an int to String in Java:
That's al about how to convert an int to String in Java. Even though its good to know multiple ways to accomplish the job, you should always be using String.valueOf() method to convert int to String. It's standard, readable and well tested. This method also take advantage of cache as most commonly used values like -128 to 127 are cached in integer pool, much like String pool in Java.
No comments:
Post a Comment