Java Long - decode() Method
The java.lang.Long.decode() method is used to decode a String into a Long. 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 Long.parseLong 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 Long decode(String nm) throws NumberFormatException
Parameters
nm |
Specify the String to decode. |
Return Value
Returns a Long object holding the long value represented by nm.
Exception
Throws NumberFormatException, if the String does not contain a parsable long.
Example:
In the example below, the java.lang.Long.decode() method is used to decode a String into a Long.
import java.lang.*; public class MyClass { public static void main(String[] args) { //creating a string holding long 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 Long object Long y1 = Long.decode(x1); Long y2 = Long.decode(x2); Long y3 = Long.decode(x3); Long y4 = Long.decode(x4); Long y5 = Long.decode(x5); //printing the Long object System.out.println("The Long object y1 is: " + y1); System.out.println("The Long object y2 is: " + y2); System.out.println("The Long object y3 is: " + y3); System.out.println("The Long object y4 is: " + y4); System.out.println("The Long object y5 is: " + y5); } }
The output of the above code will be:
The Long object y1 is: 25 The Long object y2 is: 111 The Long object y3 is: 107 The Long object y4 is: -108 The Long object y5 is: 23
❮ Java.lang - Long