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.
- Open the terminal
- Navigate to the project directory using the
cd
command
- Create a new Java file by running the following command:
touch LongDecode.java
- 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.