Let's consider the following code snippet in Java. There are some of possible approaches (that I know) to parse a String value to other numeric types (Let's say for the sake of simplicity, it is an Integer, a wrapper type in Java).
package parsing;
final public class Parsing
{
public static void main(String[] args)
{
String s="100";
Integer temp=new Integer(s);
System.out.print("\ntemp = "+temp);
temp=Integer.parseInt(s);
System.out.print("\ntemp = "+temp);
temp=Integer.valueOf(s).intValue();
System.out.print("\ntemp = "+temp);
temp=Integer.getInteger(s);
System.out.print("\ntemp = "+temp);
}
}
In all the cases except the last one, returns the value 100 after converting it into an Integer. Which one is the best approach to parse a String value to other numeric types available in Java? The last case returns NULL even if the String object s already contains a parsable value. Why?