So it is not only for converting Integer to String and String to Integer, It conversion may be for Long, Date, Enum, Double to String ,but it is similar to converting String to Integer.
There are following different ways of converting String object into int or Integer in Java :
This is method in Integer class used to converting string value to int value. This is recommended method to be used for string to int conversion. It is extremely easy and most flexible way of converting String to Integer. Let see an example of String to int conversion:
//using Integer.parseInt String str = "100" int i = Integer.parseInt(str); System.out.println("i: " + i);
Suppose str is not a proper number then this method Integer.parseInt() will throw NumberFormatException. Other wrapper classes in java has same technique to be used to convert other data type like float and Double to String in Java. It is static method in the all wrapper classes like Float.parseFloat() and Double.parseDouble() to perform data type conversion.
Integer class provide another method to converting string to integer value. This method return an Integer object holding the value represented by the string argument. This is an example of Factory method design pattern in Java and known as Integer.valueOf(), this is also a static method like main and can be used as utility for string to int conversion.
Let’s see an example of using Integer.valueOf() to convert String into int in java.
//using Integer.valueOf() String str = "100" Integer i = Integer.valueOf(str); System.out.println("i: " + i);
Suppose str is not a proper number then this method Integer.parseInt() will also throw NumberFormatException. Integer.valueOf() and Integer.parseInt(str) both are using for converting String to Integer value but there is a interesting difference between them, static valueOf() method is that it is used to create instance of wrapper class during Auto-boxing in Java So we careful whenever you want compare the value of integer after conversion with valueOf() method , it can cause subtle issues while comparing primitive to Object e.g. int to Integer using equality operator (==), because caches Integer instance in the range -128 to 127. Learn more about it.
In the above we have seen how to convert String to Integer value but here we are going explain how to convert Integer value to String value.
String str = 100 + "";
We can concatenate any number with empty String and it will create a new String. It is very simple way to convert int value to String object. Here we used “+” concatenation operator with String to convert int variable into String object.
It is very simple way but it is also one of the most poor way of converting Integer value to String. Because it is actually soil you code. When you write like String str = 100 + “” then your code translated by JVM as below:
new StringBuilder().append(100).append("").toString();
In java constructor of StringBuilder() constructs a string builder with no characters in it and an initial capacity of 16 characters. So we can append characters up to to 16 length to that StringBuilder, if appending more than 16 character will expand StringBuilder buffer. At the end, StringBuilder.toString() will create a new String object with a copy of StringBuilder buffer. That is why, it is not better way to converting Integer value to String by using empty string concatenation. For this simple way of converting a single integer value to String you will need to allocate one StringBuilder, one char array char[16], one String and one char[] of appropriate size to fit your input value. For avoiding this nonsense flow simply use String.valueOf() method because this method will not only benefit from cached set of values but also you will at least avoid creating a StringBuilder.
String price = String.valueOf(100);
Read More about StringBuffer and StringBuilder.
String Related Posts
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…