Java Long Reverse Method

JavaJavaBeginner
Practice Now

Introduction

In Java, the reverse() method is a part of the Long class of the java.lang package. This method is used to obtain the reversed binary representation of a long value. In this lab, we will learn how to use the reverse() method in Java with examples.

Create a Java Class

In this step, we will create a Java class called ReverseMethod. This class will have a main() method where we will write our Java code for the reverse() method.

public class ReverseMethod {
    public static void main(String[] args) {
        // Code for using the reverse() method will be added here.
    }
}

Use the reverse() Method - Part 1

In this step, we will use the reverse() method on a positive long value. We will print the original value and its binary representation using the toBinaryString() method. Then, we will print the new value obtained by reversing the binary representation of the original value using the reverse() method.

long number = 12345L;
System.out.println("Original Number: " + number);
System.out.println("Binary Representation: " + Long.toBinaryString(number));
long reversedNumber = Long.reverse(number);
System.out.println("Number After Reversing: " + reversedNumber);

Use the reverse() Method - Part 2

In this step, we will use the reverse() method on a negative long value. The process will be the same as that in step 2, but we will use a negative value. We will print the original value and its binary representation using the toBinaryString() method. Then, we will print the new value obtained by reversing the binary representation of the original value using the reverse() method.

long number = -12345L;
System.out.println("Original Number: " + number);
System.out.println("Binary Representation: " + Long.toBinaryString(number));
long reversedNumber = Long.reverse(number);
System.out.println("Number After Reversing: " + reversedNumber);

Use the reverse() Method - Part 3

In this step, we will create a user-defined example where anyone using this code can put a value of their choice and get the equivalent output. We will take the input from the user for the long value. We will print the original value and its binary representation using the toBinaryString() method. Then, we will print the new value obtained by reversing the binary representation of the original value using the reverse() method.

try {
    System.out.print("Enter original value: ");
    Scanner scanner = new Scanner(System.in);
    long number = scanner.nextLong();
    System.out.println("Original Number: " + number);
    System.out.println("Binary Representation: " + Long.toBinaryString(number));
    long reversedNumber = Long.reverse(number);
    System.out.println("Number After Reversing: " + reversedNumber);
} catch (Exception e) {
    System.out.println("Invalid Input: " + e);
}

Run the Code

Save the changes to the file and exit the code editor. Open the terminal and navigate to the directory where the Java code file is saved. Compile and run the Java code using the following command:

javac ReverseMethod.java && java ReverseMethod

Use the reverseBytes() Method

In this step, we will explore another variant of the reverse() method called reverseBytes(). This method is used to obtain the reversed byte order of a long value. The byte order refers to the order in which the bytes of a long value are stored in memory. We will use a positive long value for this example.

long number = 12345L;
System.out.println("Original Number: " + number);
System.out.println("Binary Representation: " + Long.toBinaryString(number));
long reversedBytes = Long.reverseBytes(number);
System.out.println("Number After Reversing Bytes: " + reversedBytes);

Use the reverseBytes() Method - Part 2

In this step, we will use the reverseBytes() method on a negative long value. The process will be the same as that in step 7, but we will use a negative value for the long value.

long number = -12345L;
System.out.println("Original Number: " + number);
System.out.println("Binary Representation: " + Long.toBinaryString(number));
long reversedBytes = Long.reverseBytes(number);
System.out.println("Number After Reversing Bytes: " + reversedBytes);

Use the reverseBytes() Method - Part 3

In this step, we will create a user-defined example where anyone using this code can put a value of their choice and get the equivalent output using the reverseBytes() method.

try {
    System.out.print("Enter original value: ");
    Scanner scanner = new Scanner(System.in);
    long number = scanner.nextLong();
    System.out.println("Original Number: " + number);
    System.out.println("Binary Representation: " + Long.toBinaryString(number));
    long reversedBytes = Long.reverseBytes(number);
    System.out.println("Number After Reversing Bytes: " + reversedBytes);
} catch (Exception e) {
    System.out.println("Invalid Input: " + e);
}

Run and Test the Code

Save the changes and exit the code editor. Compile and run the Java code using the following command:

javac ReverseMethod.java && java ReverseMethod

Enter inputs as instructed by the code. Observe the output of the program and verify that it matches the expected output.

Summary

In this lab, we learned how to use the reverse() and reverseBytes() methods in Java. We learned how to use these methods on both positive and negative long values. We also created a user-defined example where anyone using the code can put a value of their choice and get the equivalent output. We ran and tested the Java code in the terminal of an Ubuntu system.

Other Java Tutorials you may like