Java Integer.reverse() – Examples

In this tutorial, we will learn about Java Integer.reverse() method, and learn how to use this method to get an integer by reversing the order of bits of a given integer, with the help of examples.

reverse(int i)

Integer.reverse() returns the value obtained by reversing the order of the bits in the two’s complement binary representation of the specified int value.

Syntax

The syntax of reverse() method with integer as parameter is

</>
Copy
Integer.reverse(int i)

where

ParameterDescription
iThe integer value to be reversed.

Returns

The method returns value of type int.

Example 1 – reverse(int)

In this example, we will take an integer and reverse it using Integer.reverse(int) method.

Java Program

</>
Copy
public class Example {
	public static void main(String[] args){
		int i = 12340;
		int result = Integer.reverse(i);
		System.out.println("Result of reverse("+i+") = " + result);
	}
}

Output

Result of reverse(12340) = 738983936

Conclusion

In this Java Tutorial, we have learnt the syntax of Java Integer.reverse() method, and also how to use this method with the help of Java example programs.