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.
Syntax :
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.
Examples:
Input: str_value = "50" Output: 50 Input: str_value = "GFG" Output: NumberFormatException
Below programs illustrate the java.lang.Integer.decode() method.
Program 1:
// Java program to demonstrate working // of java.lang.Integer.decode() method import java.lang.*; public class Gfg { public static void main(String[] args) { Integer int1 = new Integer(22); // string here given the value of 65 String nstr = "65"; // Returns an Integer object holding the int value System.out.println("Actual Integral Number = "+ int1.decode(nstr)); } } |
Actual Integral Number = 65
Program 2: When string value is passed a NumberFormatException is thrown.
// Java program to demonstrate working // of java.lang.Integer.decode() method import java.lang.*; public class Gfg { public static void main(String[] args) { Integer int1 = new Integer(22); // String here passed as "geeksforgeeks" String nstr = "geeksforgeeks"; System.out.println("Actual Integral Number = "); System.out.println(int1.decode(nstr)); } } |
Output:
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)
Recommended Posts:
- Java Long decode() method with Examples
- Integer sum() Method in Java
- Java Integer byteValue() method
- Java Integer bitCount() method
- Integer shortValue() Method in Java
- Integer rotateRight() Method in Java
- Integer reverseBytes() Method in Java
- Java Integer compareTo() method
- Integer signum() Method in Java
- Integer valueOf() Method in Java
- Integer.numberOfLeadingZeros() Method in Java With Example
- Integer hashCode() Method in Java
- Integer highestOneBit() Method in Java
- Java Integer compareUnsigned() method
- Integer doubleValue() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



