here are different ways of converting String object into Integer in Java :
1) By using Intger.parseInt(String string-to-be-converted) method
This is my preferred way of converting it, Extremely easy and most flexible way of converting String to Integer. Let see an example of String to int conversion:
Integer.parseInt() method will throw NumberFormatException if String provided is not a proper number. Same technique can be used to convert other data type like float and Double to String in Java. Java API provides static methods like Float.parseFloat() and Double.parseDouble() to perform data type conversion.
2) There is another way of converting String into Integer which was hidden to me for long time mostly because I was satisfied with Integer.parseInt() method. 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.
It will ignore the leading zeros and convert the string into int. This method also throws NumberFormatException if string provided does not represent actual number. Another interesting point about static valueOf() method is that it is used to create instance of wrapper class during Autoboxing in Java and 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.
1) By using Intger.parseInt(String string-to-be-converted) method
This is my preferred way of converting it, Extremely easy and most flexible way of converting String to Integer. Let see an example of String to int conversion:
//using Integer.parseInt
int i = Integer.parseInt("123");
System.out.println("i: " + i);
Integer.parseInt() method will throw NumberFormatException if String provided is not a proper number. Same technique can be used to convert other data type like float and Double to String in Java. Java API provides static methods like Float.parseFloat() and Double.parseDouble() to perform data type conversion.
2) There is another way of converting String into Integer which was hidden to me for long time mostly because I was satisfied with Integer.parseInt() method. 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.
//How to convert numeric string = "000000081" into Integer value = 81
int i = Integer.parseInt("000000081");
System.out.println("i: " + i);
It will ignore the leading zeros and convert the string into int. This method also throws NumberFormatException if string provided does not represent actual number. Another interesting point about static valueOf() method is that it is used to create instance of wrapper class during Autoboxing in Java and 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.
No comments:
Post a Comment