Integer decode() Method in Java
It is often seen that any integer within (” “) is also considered as string, then it is needed to decode that into the integer. The main function of java.lang.Integer.decode() method is to decode a String into an Integer. The method also accepts decimal, hexadecimal, and octal numbers.
public static Integer decode(String str)
Parameters: The method takes one parameter str of String data type and refers to the string needed to decode.
Return Value: This method returns an Integer object which holds the int value represented by the string str.
Exception: The method throws NumberFormatException, when the String contains an integer that cannot be parsed.
Input: str_value = "50" Output: 50 Input: str_value = "GFG" Output: NumberFormatException
Below programs illustrate the java.lang.Integer.decode() method.
Actual Integral Number = 65
Program 2: When string value is passed a NumberFormatException is thrown.
Exception in thread "main" java.lang.NumberFormatException: For input string: "geeksforgeeks" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.valueOf(Integer.java:740) at java.lang.Integer.decode(Integer.java:1197) at Gfg.main(Gfg.java:15)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.