Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Long decode() Method

Java decode() method belongs to the Long class. The decode() method is used to decode a string of the form " " into a long.

The numbers that are acceptable are:

  • decimal

  • hexadecimal

  • octal

Syntax:

public static Long decode(String nm) throws NumberFormatException

Parameter:

The parameter includes the String nm which is to be decoded into Long.

Returns:

the long value of the Long object which the String nm holds.

The NumberFormatException is thrown in case the string does not contain any parsable long.

Example 1:

Here, the number starting with '0' will be decoded as octal using the base 8, starting with '0x' will be decoded as hexadecimal using the base 16 and starting with '-0x' will be decoded as signed hexadecimal using the base 16.

import java.lang.Long;
public class StudyTonight
{  
    public static void main(String[] args)
    {          
        Long i = 30L; 
         
        String s = "300";    
      
        System.out.println(Long.decode("05234"));   // decoding using base 8             
                        
        System.out.println(Long.decode("0x980"));   // decoding using base 16               
                        
        System.out.println(Long.decode("-0x786"));  // decode using base 16 signed 
        
        System.out.println(" Decoded Long Number for the given string is = " +i.decode(s)); 
        // returns the long value of the Long object which the String s holds.
    }  
} 


2716
2432
-1926
Decoded Long Number for the given string is = 300

Example 2:

Here, we come across NumberFormatException as the string s cannot be decoded into a long.

import java.lang.Long;
public class StudyTonight
{  
    public static void main(String[] args)
    {  
        Long i = 100L;          
        String s = "StudyTonight";             
        System.out.println(" Decoded long number is = " + i.decode(s));   
    }  
}  


Exception in thread "main" java.lang.NumberFormatException: For input string: "StudyTonight"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.valueOf(Long.java:776)
at java.lang.Long.decode(Long.java:928)
at StudyTonight.main(StudyTonight.java:8)

Example 3:

Here is a general example where the user can enter the numbers of his choice and get the desired output. The try and catch block is used for handling any exception taking place during the execution of the program.

import java.lang.Long;
import java.util.Scanner;  
public class StudyTonight
{  
    public static void main(String[] args)
    {  
        try
        {
            System.out.print("Enter the string to be decoded: ");  
            Scanner sc = new Scanner(System.in);  
            String s = sc.nextLine();  
            Long decoded = Long.decode(s);  
            System.out.println("Decoded value is:" + decoded);  
        }
        catch(Exception e)
        {
            System.out.println("Number Format Exception,Please Check the Entered String");
        }
    }  
}  


Enter the string to be decoded: 0x6745
Decoded value is:26437
***********************************************
Enter the string to be decoded: -0x587
Decoded value is:-1415
***********************************************
Enter the string to be decoded: mohit
Number Format Exception,Please Check the Entered String

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.