Java Long decode() method with Examples
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.
// Java program to demonstrate // of java.lang.Long.decode() method import java.lang.Math; class GFG { // driver code public static void main(String args[]) { // demonstration of function Long l = new Long(14); String str = "54534"; System.out.println("Number = " + l.decode(str)); } } |
Output:
Number = 54534
Program 2: The program demonstrates the conversions using decode() function
// Java program to demonstrate // of java.lang.Long.decode() method import java.lang.Math; class GFG { // driver code public static void main(String args[]) { // demonstration of conversions String decimal = "10"; // Decimal String hexa = "0XFF"; // Hexa String octal = "067"; // Octal // convert decimal val to number using decode() method Integer number = Integer.decode(decimal); System.out.println("Decimal [" + decimal + "] = " + number); number = Integer.decode(hexa); System.out.println("Hexa [" + hexa + "] = " + number); number = Integer.decode(octal); System.out.println("Octal [" + octal + "] = " + number); } } |
Output:
Decimal [10] = 10 Hexa [0XFF] = 255 Octal [067] = 55
Program 3: The program demonstrates error and exceptions.
// Java program to demonstrate // of java.lang.Long.decode() method import java.lang.Math; class GFG { // driver code public static void main(String args[]) { // demonstration of errorand exception when // a non-parsable Long is passed String decimal = "1A"; // throws an error Integer number = Integer.decode(decimal); System.out.println("string [" + decimal + "] = " + number); } } |
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)
Recommended Posts:
- Java Guava | LongMath.divide(long, long, RoundingMode) method with Examples
- Java Guava | Longs.checkedSubtract(long a, long b) method with Examples
- Java Guava | LongMath.checkedAdd(long a, long b) method with Examples
- Java Guava | Longs.indexOf(long[] array, long[] target) method with Examples
- Java Guava | Longs.indexOf(long[] array, long target) method with Examples
- Duration ofSeconds(long, long) method in Java with Examples
- Java Long equals() method with Examples
- OptionalLong of(long) method in Java with examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
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.

