java.lang.Long decode(String nm)

Description

The Long.decode(String nm) java method decodes 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:

+

Make a note that the decode() method of Long class is static thus it should be accessed statically which means the we would be calling this method in this format:

Long.decode(method args)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java decode method non statically.

Notes:

  • this method throws NumberFormatException, if the String does not contain a parsable long.

Method Syntax

public static Long decode(String nm) throws NumberFormatException

Method Argument

Data Type Parameter Description
String nm the String to decode.

Method Returns

The decode(String nm) method of Long class returns a Long object holding the long value represented by nm

Compatibility

Requires Java 1.2 and up

Java Long decode() Example

Below is a simple java example on the usage of decode() method of Long class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of  
 * decode(String nm) method of Long class
 */

public class LongDecodeExample {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("Enter a value:");

		// declare a scanner object to read the user input
		Scanner s = new Scanner(System.in);

		// assign the input to a variable
		String value = s.nextLine();

		// get the decode() method result
		Long result = Long.decode(value);

		// print the result
		System.out.println("Result:" + result);

		// close the scanner object
		s.close();

		/*
		 * Format available 
		 * 0x HexDigits 
		 * 0X HexDigits 
		 * # HexDigits 
		 * 0 OctalDigits
		 */
	}

}

Sample Output

Below is the sample output when you run the above example.

Java Long decode(String nm) example output