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.


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/packages_api("`Packages / API`") 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/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-117736{{"`Java Integer Reverse Method`"}} java/classes_objects -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/class_methods -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/exceptions -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/modifiers -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/oop -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/packages_api -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/user_input -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/wrapper_classes -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/identifier -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/arrays -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/data_types -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/operators -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/output -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/strings -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/variables -.-> lab-117736{{"`Java Integer Reverse Method`"}} java/system_methods -.-> lab-117736{{"`Java Integer Reverse Method`"}} end

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