4 ways to convert int value to String in Java? Examples

You can use String.valueOf() or Integer.toString() method to convert an int value to String object in Java. Remember, String is not just a char array but a full feature object in Java. The standard way to convert one object to String in Java is just calling the toString() method, but since here we need to convert primitive int to String, I prefer to use String.valueOf(int) method. It reads better because we are converting an int to String and not an Integer to String. There is a difference between them, int is a primitive value while Integer is a wrapper object in Java. BTW, there is one more way to convert an int value to String in Java, the easiest one, String concatenation. Even though Java doesn't support operator overloading, + operator behaves differently with integer and String operands. If one of the argument to this binary operator is String then it performs String concatenation, otherwise, it does addition. So you can convert an int value e.g. 10 to a String value "10" by just doing "" + 10.

Btw, I don't recommend that as preferred way because it looks like code smell and there is a chance of error if there is more argument e.g. 10 + 10 + "" will produce "20" and not "1010" because + is right associative operator and it will first perform addition of 10 + 10 and then String concatenation between 20 + "".




4 Ways to Convert an int value to a String in Java


Solution 1 - String.valueOf() 

It's no doubt my favorite way for int to String conversion because you can use the same approach to convert other data types to String as well e.g. you can call String.valueOf(long) to convert a long to String, String.valueOf(double) to convert a double value to String and String.valueOf(float) to convert a float value to String in Java.

Most importantly It reads well. It's also a good example of the static factory method, which provides some significant advantages over constructor for creating an object. Btw, Internally valueOf() also calls the Integer.toString() method for conversion, as shown below:

public static String valueOf(int i) {
  return Integer.toString(i, 10);
}

Now let's see how to use this method, here is an example of using String.valueOf() for int to String conversion in Java:

String hundred = String.valueOf(100); // you can pass int constant

int ten = 10;
String ten = String.valueOf(ten) // You can also pass int variable


Solution 2 - Integer.toString()

Another way to convert an int value to String is by using Integer.toString() method. This method converts the passed argument into int primitive as shown below:

String two = Integer.toString(20); // returns "20"
String one = Integer.toString(01); // returns "1"
String twentyOne = Integer.toString(-21); // returns -21
String seven = Integer.toString(+7); // returns 7

You can pass this method a signed int value but it will preserve sign only for the negative number. Even if you pass +7, it will create String as "7" without plus sign.

Integer.toString() by default converts int to String in decimal format i.e. base 10, if you want to convert int to String in any other number system e.g. binary, octal or hexadecimal you can use overloaded version of Integer.toString(int value, int radix) which also accepts a radix. You can pass radix as 16 to convert int to hexadecimal String, 8 to convert int value to Octal String and 2 to convert int value to binary String as shown in the following example:

String hexa = Integer.toString(20,16); // "14"
String ocatal = Integer.toString(20, 8); // "24"
String binary = Integer.toString(20, 2); // "10100"

So, if you want to convert int to String in the different number system, you can use Integer.toString() for that purpose.


Solution 3 - String concatenation

This is the easiest way to convert an int primitive to String object in Java. All you need to do is concatenate the int value with an empty String e.g. "" + 100 and this will return "100", no error, no issue, simple and easy, but I don't like that in production code? Why? isn't it most readable as well? yes, it does but somehow it looks like a hack to me than a proper solution. another reason is that it internally uses StringBuilder for concatenation, which means one more temporary object for GC to cleanup. Nevertheless, if you like you can use this approach, especially during testing.

String thousand = "" + 1000;
String five = "" + 5;


Solution 4 - String.format() method

The format() method was introduced in java.lang.String class on JDK 1.5 and its primary use of nothing but to format numbers int to String. You can use also use this method to simply convert an int value to String as shown below:

String million = String.format("%d", 1000000)

It's especially useful for converting large int values to String. For more examples of formatting String in Java, see here.

How to convert int value to String object in Java

Java Program to convert int to String in Java

Here is our sample Java program to convert an int primitive to String object. In this example, you will learn all three ways and one bonus way.

/**
 * Java Program to convert an int value to String object. In this article, we
 * will take a look at 4 different ways of converting int to String in Java. i)
 * String.valueOf() ii) Integer.toString() iii) String concatenation iv) String
 * format() method
 *
 * @author WINDOWS 8
 */
public class Int2String {

    public static void main(String args[]) {

        // 1st Example - by using String.valueOf() method
        String hundred = String.valueOf(100); // you can pass int constant
        int ten = 10;
        String str = String.valueOf(ten); // You can also pass int variable

        // 2nd Example - by using Integer.toString()
        String two = Integer.toString(20); // returns "20"
        String one = Integer.toString(01); // returns "1"
        String twentyOne = Integer.toString(-21); // returns -21
        String seven = Integer.toString(+7); // returns 7

        // 3rd Example - by using String concatenation
        String thousand = "" + 1000;
        String five = "" + 5;

        // 4th Example - by using String format() method
        String million = String.format("%d", 1000000);

    }

}


That's all about how to convert an int value to String in Java. As I said, you can use either Integer.toString(), String.valueOf() or simply concatenation with empty String to do this task. Though it's better to use String.valueOf() method, it's more readable and also the standard way to convert any primitive data type to String in Java. You can also use String.format() to get your int value as nicely formatted String, especially suitable to convert large int values e.g. 1,000,000


No comments:

Post a Comment