Java Integer reverseBytes Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the reverseBytes() method of the Java Integer class. This method is used to return the value which is obtained by reversing the order of the bytes of the two's complement binary representation of the integer value passed.


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/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") 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-117738{{"`Java Integer reverseBytes Method`"}} java/exceptions -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} java/oop -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} java/packages_api -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} java/user_input -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} java/wrapper_classes -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} java/identifier -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} java/data_types -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} java/operators -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} java/output -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} java/strings -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} java/variables -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} java/system_methods -.-> lab-117738{{"`Java Integer reverseBytes Method`"}} end

Set up the Code file

Create a new file named ReverseBytesExample.java and open it in any text editor of your choice.

In the first line, define the package name for the class. For example,

package com.example.reversebytes;

In the next line, import the java.lang.Integer class to your file using the following code:

import java.lang.Integer;

Implement the Code

In this step, we will write code to demonstrate the working of the reverseBytes() method.

Add the following code inside the main() method to demonstrate the working of the method:

int a = 342;
int b = -23;
System.out.println("Original Number = " + a);
System.out.println("Binary Representation is = " + Integer.toBinaryString(a));
System.out.println("Number after reversal " + Integer.reverseBytes(a));

System.out.println("\nOriginal Number = " + b);
System.out.println("Binary Representation is = " + Integer.toBinaryString(b));
System.out.println("Number after reversal = " + Integer.reverseBytes(b));

We first define two integer variables, a and b. We then print out the original number, its binary representation, and the number obtained after reversing its bytes using the reverseBytes() method. We do this for both a and b.

Next, we will take user input to demonstrate the reverseBytes() method for user-defined values. Add the following code inside the main() method:

try {
    System.out.print("Enter Original Value: ");
    Scanner sc = new Scanner(System.in);
    int i = sc.nextInt();
    System.out.println("Actual Number = " + i);
    System.out.println("Binary Representation = " + Integer.toBinaryString(i));
    System.out.println("After reversing = " + Integer.reverseBytes(i));
} catch(Exception e) {
    System.out.println("Invalid Input");
}

Here, we define a try-catch block to handle any exceptions that may arise. We take user input using the Scanner class, print out the original number entered by the user, its binary representation, and the number obtained after reversing its bytes using the reverseBytes() method.

Run the Code

Save the ReverseBytesExample.java file.

Open the terminal and navigate to the directory in which the file is saved. Compile the code using the following command:

javac ReverseBytesExample.java

Then, run the code using the following command:

java ReverseBytesExample

You should see the output for the predefined values and should be able to enter values of your choice as well.

Summary

In this lab, we learned how to use the reverseBytes() method of the Java Integer class. We learned how to demonstrate the working of the reverseBytes() method for predefined values as well as how to take user input to test the method on user-defined values. You can now use this method in your Java programs to reverse the bytes of an integer value.

Other Java Tutorials you may like