# How To Reverse an Integer in Java

How To Reverse An Integer In Java | In this blog, we will see how to reverse an integer value in Java programming language. We can develop our custom method or we can take the help of the pre-defined methods available in the Integer class.

The reverse() method given in the Integer class is used to reverse an integer object by using the two’s complement in binary representation. But let us first see our own custom method for it.

## Reverse an Integer In Java

To reverse a given number in Java, we have to iterate through the digits of the number. In each iteration, we have to find the last digit and remove that last digit. To find the last digit we can use the % operator whereas to remove the last digit we can use the / operator.

``````public class Main {
public static void main(String[] args) {
int number = 123456;
System.out.println("Number: " + number);
int newNumber = reverse(number);
System.out.println("After reverse: " + newNumber);

int number1 = -95862;
System.out.println("Number: " + number1);
System.out.println("After reverse: " + reverse(number1));

}

public static int reverse(int number) {
// if number is negative then make positive
boolean isNegative = false;
if (number < 0) {
isNegative = true;
number *= -1;
}

int reverse = 0;
while (number != 0) {
// get last digit
int lastDigit = number % 10;
reverse = (reverse * 10) + lastDigit;
// remove last digit from original number
number /= 10;
}

if (isNegative) {
return -1 * reverse;
}

return reverse;
}
}``````

Output:-

Number: 123456
After reverse: 654321
Number: -95862
After reverse: -26859

In the previous example we have reversed an int primitive value, now let us see how to reverse an Integer object using the same method.

When we assign an int primitive value to an Integer object then due to Java auto-boxing feature the primitive value is wrapped as an object. Similarly, when we assign an Integer object value to an int primitive value then due to auto-unboxing the Object value is unwrapped to the primitive value.

``````Integer number = 15645;
System.out.println("Number: " + number);
Integer newNumber = reverse(number);
System.out.println("After reverse: " + newNumber);``````

Output:-

Number: 15645
After reverse: 54651

## Reverse an Integer In Java using Integer.reverse() Method

The syntax for the reverse() method is as follows:- public static int reverse(int number)
Parameters: number, the integer value which has to be reversed.
Return type: int
Returns: the number after reversing.

The below code explains to you how to reverse both positive and negative integers. It returns the value obtained by reversing the order of the bits in the two’s complement binary representation of the specified int value.

``````public class Main {
public static void main(String[] args) {
int number = 1000;
System.out.println("Number: " + number);
System.out.println("After reversing: " + Integer.reverse(number));

number = -5222;
System.out.println("Number: " + number);
System.out.println("After reversing: " + Integer.reverse(number));
}
}``````

Output:-

Number: 1000
After reversing: 398458880
Number: -5222
After reversing: 1507327999

## How To Reverse Integer In Java using reverseBytes() Method

Now we will see the working of the method reverseBytes(). This method returns the value obtained by reversing the order of the bytes in the two’s complement representation of the specified int value.

``````public class Main {
public static void main(String[] args) {
int number = 1000;
System.out.println("Number: " + number);
System.out.println("After reversing: " + Integer.reverseBytes(number));

number = -5222;
System.out.println("Number: " + number);
System.out.println("After reversing: " + Integer.reverseBytes(number));
}
}``````

Output:-

Number: 1000
After reversing: -402456576
Number: -5222
After reversing: -1695809537

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!