Javatpoint Logo
Javatpoint Logo

Java Integer decode() method

The decode() method is a static method of Integer class under java.lang package. The main function of this method is to decode a string which is written in the form of (" ") into an integer value.

The decode() method also accepts decimal, hexadecimal, and octal numbers. It is given by the following grammar:

DecodableString:

Signopt DecimalNumeral Signopt 0x HexDigits Signopt 0X HexDigits Signopt # HexDigits Signopt 0 OctalDigits

Sign:

This type of String will throw a NumberFormatException or represent a positive value. The result is negated if sign of the first character of the decodable String is minus. There are no whitespace characters are permitted in the String.

Syntax

Following is the declaration of decode() method:

Parameter:

DataType Parameter Description Required/Optional
String nm The value which we want to decode into an Integer object Required

Returns:

The decode() method returns an Integer object which hold the int value represented by String nm.

Exceptions:

NumberFormatException - This method throws an exception if the String does not contain an integer which can be parsed.

Compatibility Version:

Java 1.2 and above

Example 1

Test it Now

Output:

81
276
-260

Example 2

Test it Now

Output:

Integer Number is = 555

Example 3

This is an example in which we pass the String value and it shows NumberFormatException.

Test it Now

Output:

Exception in thread "main"java.lang.NumberFormatException: For input string: "JavaTpoint"
	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 myPackage.DecodeMethodExample.main(DecodeMethodExample.java:8)

Example 4

Output:

Enter a value to decode: 656
Result:656





Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA