Best way to convert numbers to String in Java

Number to String conversion in Java
There are many ways to convert numbers to String in Java, Some of them we saw on How to convert int to String and  converting double to String, But out of those which is the best way to convert number to String in Java?  We will cover in this post. If you look at those Java tutorial you will find that I have advised for using String.valueOf()method for converting number to String, which is in my opinion best way to convert number to String. here are few reason Why valueOf() should be your preferred option when it comes to converting numbers to String in Java. Since converting from one class to another, especially from String to other data type or other class is quite common in Java programming world, we have already covered something like How to convert Enum to String in Java  and  String to Date conversion in Java. If you are interested in getting yourself comfortable with converting String to other useful objects, I would recommend those articles. Anyway let’s come back to number to String conversion.
Look at this example :

Set<String> cities = new HashSet<String>();

String size = "" + cities.size()

Best way to convert number to String in Java with ExampleIn this example we want to convert size of Collection i.e int to String in Java and we have used most  simplest way by concatenating with empty String. This approach, on surface looks clean and easy but its not the best way to convert numbers to String in Java. As discussed in String vs StringBuffer, String concatenation is replaced by either StringBuffer or StringBuilder e.g. something like

String size = new StringBuilder().append(cities.size).toString();

Which involves an extra object StringBuilder and if you are doing this operation on a loop or frequently called method, you are bound to create lot of garbage and lot of work for Garbage collector in Java.

Number to String Conversion in Java

On the other hand if we use String.valueOf() , which is overloaded for int, long, float and double and can be used to convert any number to String, we will get following advantages :

1) String.valueOf() is overloaded for different numeric data type, which means provides a consistent way to convert number to String in Java.

2) String.valueOf() does not incur cost of intermediate concatenation, which means no temporary object e.g. StringBuilder or StringBuffer.

3) valueOf() is made for conversion purpose and you will find this method in almost all value classes e.g. String, Integer, Float, Double, Long, BigDecimal etc. Using valueOf() keeps you code consistent across conversion of one data type to another. In case of Integer, valueOf() method also able to cache frequently used numbers e.g. from -127 to 128. In short using valueOf() promotes best practice in Java.

4) valueOf() delegates String conversion to respective classes toString() method in Java e.g. valueOf(int i) delegates to Integer.toString(i, 10) which means String representation of numbers will be consistent

These were some of the reason Why I prefer valueOf() method to convert numbers to String and thing its best way to convert numbers like int, long, float or double into String. Next time you convert number to String, consider using String.valueOf() if you are not using it already.

Other Java articles based on String you may like
How to split String in Java with Regular expression                       

No comments:

Post a Comment