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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117910{{"`Java Long Reverse Method`"}} java/classes_objects -.-> lab-117910{{"`Java Long Reverse Method`"}} java/class_methods -.-> lab-117910{{"`Java Long Reverse Method`"}} java/exceptions -.-> lab-117910{{"`Java Long Reverse Method`"}} java/modifiers -.-> lab-117910{{"`Java Long Reverse Method`"}} java/oop -.-> lab-117910{{"`Java Long Reverse Method`"}} java/user_input -.-> lab-117910{{"`Java Long Reverse Method`"}} java/wrapper_classes -.-> lab-117910{{"`Java Long Reverse Method`"}} java/identifier -.-> lab-117910{{"`Java Long Reverse Method`"}} java/arrays -.-> lab-117910{{"`Java Long Reverse Method`"}} java/comments -.-> lab-117910{{"`Java Long Reverse Method`"}} java/data_types -.-> lab-117910{{"`Java Long Reverse Method`"}} java/operators -.-> lab-117910{{"`Java Long Reverse Method`"}} java/output -.-> lab-117910{{"`Java Long Reverse Method`"}} java/strings -.-> lab-117910{{"`Java Long Reverse Method`"}} java/variables -.-> lab-117910{{"`Java Long Reverse Method`"}} java/system_methods -.-> lab-117910{{"`Java Long Reverse Method`"}} end

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