Java Integer - decode() Method
The java.lang.Integer.decode() method is used to decode a String into an Integer. Accepts decimal, hexadecimal, and octal numbers given by the following grammar:
DecodableString:
- Signopt DecimalNumeral
- Signopt 0x HexDigits
- Signopt 0X HexDigits
- Signopt # HexDigits
- Signopt 0 OctalDigits
Sign:
- +
- -
The sequence of characters following an optional sign and/or radix specifier ("0x", "0X", "#", or leading zero) is parsed as by the Integer.parseInt method with the indicated radix (10, 16, or 8). This sequence of characters must represent a positive value or a NumberFormatException will be thrown. The result is negated if first character of the specified String is the minus sign. No whitespace characters are permitted in the String.
Syntax
public static Integer decode(String nm) throws NumberFormatException
Parameters
nm |
Specify the String to decode. |
Return Value
Returns an Integer object holding the int value represented by nm.
Exception
Throws NumberFormatException, if the String does not contain a parsable integer.
Example:
In the example below, the java.lang.Integer.decode() method is used to decode a String into an Integer.
import java.lang.*; public class MyClass { public static void main(String[] args) { //creating a string holding int value String x1 = "25"; //decimal number String x2 = "0x6f"; //hexadecimal number String x3 = "0X6B"; //hexadecimal number String x4 = "-#6c"; //hexadecimal number String x5 = "027"; //octal number //creating Integer object Integer y1 = Integer.decode(x1); Integer y2 = Integer.decode(x2); Integer y3 = Integer.decode(x3); Integer y4 = Integer.decode(x4); Integer y5 = Integer.decode(x5); //printing the Integer object System.out.println("The Integer object y1 is: " + y1); System.out.println("The Integer object y2 is: " + y2); System.out.println("The Integer object y3 is: " + y3); System.out.println("The Integer object y4 is: " + y4); System.out.println("The Integer object y5 is: " + y5); } }
The output of the above code will be:
The Integer object y1 is: 25 The Integer object y2 is: 111 The Integer object y3 is: 107 The Integer object y4 is: -108 The Integer object y5 is: 23
❮ Java.lang - Integer