VOOZH about

URL: https://www.geeksforgeeks.org/java/java-long-decode-method-with-examples/

⇱ Java Long decode() method with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Long decode() method with Examples

Last Updated : 5 Dec, 2018
The java.lang.Long.decode() is a built in function in java that decodes a String into a Long. It accepts decimal, hexadecimal, and octal numbers. Syntax:
public static Long decode(String number) throws NumberFormatException 

Parameter: 
number-  the number that has to be decoded into a Long. 
Error and Exceptions:
  • NumberFormatException: If the String does not contain a parsable long, the program returns this error.
Returns: It returns the decoded string. Program 1: The program below demonstrates the working of function. Output:
Number = 54534 
Program 2: The program demonstrates the conversions using decode() function Output:
Decimal [10] = 10
Hexa [0XFF] = 255
Octal [067] = 55
Program 3: The program demonstrates error and exceptions.
Output:
 
Exception in thread "main" java.lang.NumberFormatException: For input string: "1A"
 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(File.java:16)

Comment