Java Long Decode Method

JavaJavaBeginner
Practice Now

Introduction

The decode() method is a built-in Java method that is used to convert a string representation of a number into its long value. The numbers that can be decoded include decimal, hexadecimal, and octal numbers. If the string cannot be converted into a long, a NumberFormatException is thrown.

Create a Java program

Now, let's create a Java program that uses the decode() method to convert a string representation of a number into a long value.

  1. Open the terminal
  2. Navigate to the project directory using the cd command
  3. Create a new Java file by running the following command: touch LongDecode.java
  4. In the editor, type the following code:
public class LongDecode {
    public static void main(String[] args) {
        String hex = "0x4a";
        long value = Long.decode(hex);
        System.out.println("Decimal value of " + hex + " is " + value);

        String octal = "0573";
        value = Long.decode(octal);
        System.out.println("Decimal value of " + octal + " is " + value);

        String decimal = "1234";
        value = Long.decode(decimal);
        System.out.println("Decimal value of " + decimal + " is " + value);
    }
}

This code creates a class named LongDecode with a main method that uses the decode() method to convert a hexadecimal, octal, and decimal string representation of a number into its long value. It then prints each long value to the console.

Compile and run the Java program

Now that we have written the Java program, let's compile and run it to see the output.

  1. Save the file by pressing Ctrl + X, then Y, and then Enter
  2. Compile the Java program by running the following command: javac LongDecode.java
  3. Run the Java program by running the following command: java LongDecode

The output of the program will be:

Decimal value of 0x4a is 74
Decimal value of 0573 is 371
Decimal value of 1234 is 1234

Use the decode() method in your Java code

You can use the decode() method in your own Java code to convert a string representation of a number into its long value. Here's an example of how you can do this:

public class MyCode {
    public static void main(String[] args) {
        String num = "-0x2a";
        long longValue = Long.decode(num);
        System.out.println("The long value of " + num + " is " + longValue);
    }
}

This code creates a class named MyCode with a main method that takes a string representation of a number and uses the decode() method to convert it into a long value. It then prints the long value to the console.

Run your Java code

To run your Java code, follow these steps:

  1. Save the file
  2. Compile the Java program by running javac MyCode.java
  3. Run the Java program by running java MyCode

The output of the program will be:

The long value of -0x2a is -42

Summary

In this lab, we learned how to use the decode() method in Java to convert a string representation of a number into its long value. We created a Java program to demonstrate its usage and then learned how to use it in our own Java code. By the end of this lab, you should be able to use the decode() method in your Java programs to convert numbers from string representations into their long values.

Other Java Tutorials you may like