To add on - It is always a good idea to surround the parseInt method with a try catch block to ensure that the parsing process doesnt break the code esp in cases where the value being converted is a user input run time) Instead ofĪlso there's another way to convert String to Integer using a constructor:īut I guess that this is dumb, because there's autoboxing involved, so I always use static method Integer.parseInt() Here is an example of int to String using String.valueOf() Static binding is used to call the corresponding method in Java. In-fact String.valueOf() method is overloaded to accept almost all primitive type so you can use it convert char, double, float or any other data type into String. In this example of converting Integer to String we have used String.valueOf() method which is another static utility method to convert any integer value to String. One more example of converting int to String There are many ways to convert an int variable into String, In case if you don't like above example of string conversion than here is one more example of doing the same thing. To learn more about these two, see my post about StringBuffer vs StringBuilder vs String If you use String.vauleOf() will not only benefit from a cached set of values but also you will at least avoid creating a StringBuilder. This means for converting a single integer value to String you will need to allocate: one StringBuilder, one char array char, one String and one char of appropriate size to fit your input value. Though it's not going to happen because Integer.MAX_VALUE is 2147483647, which is less than 16 characters.Īt the end, StringBuilder.toString() will create a new String object with a copy of the StringBuilder buffer. So, appending up to 16 characters to that StringBuilder will not require buffer reallocation, but appending more than 16 characters will expand StringBuider buffer. StringBuilder(String) constructor allocates a buffer containing 16 characters. You don't have to do anything special just use "+" concatenation operator with String to convert int variable into String object as shown in the following example: Int to String in Java using "+" operator Anything could not be more easy and simple than this. anyway, there are a couple of more ways to convert int into String and we will see those here with examples.ġ. Under the carpet + operator uses either StringBuffer or StringBuilder to concatenate String in Java. You can simply concatenate any number with empty String and it will create a new String. In my opinion this is simpler than previous one. How to convert Integer to String in Java? Example In the previous example of this String to int conversion we have seen changing String value into int primitive type and this part of Java tutorial we will see opposite i.e. int to Integer using equality operator (=), because of caches Integer instance in the range -128 to 127. Another interesting point about static valueOf() method is that it is used to create an instance of wrapper class during Autoboxing in Java and can cause subtle issues while comparing primitive to Object e.g.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |