Java Integer Reverse Method

JavaJavaBeginner
Practice Now

Introduction

In Java, reverse() method is used to return the value obtained by reversing the order of the bits of the two's complement binary representation of the integer value passed. In this lab, we will learn how to use the reverse() method, and we will implement it in some examples.

Write the example program

Write the following code block to implement example program:

import java.lang.Integer;

public class IntegerReverse {
    public static void main(String[] args) {
        int positiveValue = 202;
        int negativeValue = -50;

        System.out.println("Original Positive Number:" + positiveValue);
        System.out.println("Binary Representation:" + Integer.toBinaryString(positiveValue));
        System.out.println("Number after reversal:" + Integer.reverse(positiveValue));

        System.out.println("\nOriginal Negative Number:" + negativeValue);
        System.out.println("Binary Representation:" + Integer.toBinaryString(negativeValue));
        System.out.println("Number after reversal:" + Integer.reverse(negativeValue));
    }
}

In this step, we have learned to import the java.lang.Integer package into our class. We have defined two integer variables and assigned them with values.

Using the toBinaryString() method, we have converted these values into their binary numbers by its calling inside the System.out.println() method. Then we have called the reverse() method on these two variables to get their reversed binary representation.

Compile and run the program

Compile and run the program by executing the following command in the terminal.

javac IntegerReverse.java && java IntegerReverse

The output will look like this:

Original Positive Number:202
Binary Representation:11001010
Number after reversal:1392508928

Original Negative Number:-50
Binary Representation:11111111111111111111111111001110
Number after reversal:1946157055

Implement user-defined program

Now we will write a user-defined program, which takes int as input from the user and returns the output of the reverse() method. Write the following code block:

import java.util.Scanner;

public class IntegerReverse {
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter any integer value: ");
            int inputValue = scanner.nextInt();

            System.out.println("Original Number:" + inputValue);
            System.out.println("Binary Representation:" + Integer.toBinaryString(inputValue));
            System.out.println("Number after reversal:" + Integer.reverse(inputValue));
        } catch (Exception e) {
            System.out.println("Invalid Input");
        }
    }
}

In this program, we have used the Scanner class to get the input from the user. We have printed a message to the user asking them to enter any integer value. After getting the input value, we have printed the original number as well as its binary representation. Finally, we have applied the reverse() method to the input value and printed the result.

Compile and run the program

Compile and run the program by executing the following command in the terminal.

javac IntegerReverse.java && java IntegerReverse

You will see the following output:

Enter any integer value: 78
Original Number:78
Binary Representation:1001110
Number after reversal:1912602624

Test the program with invalid input

Now test the program by entering invalid inputs (such as non-integer values).

Enter any integer value: abcd
Invalid Input

This message indicates that the input value was not a valid integer value.

Summary

In this lab, we have learned how to use the reverse() method in Java to reverse the binary representation of an integer value. We have implemented two examples, one provided example, and one user-defined example to illustrate the use of this method. We have used the Scanner class to get input value from the user.

Other Java Tutorials you may like