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.

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