The reverse()
method in Java is a built-in function of the StringBuilder
class that is used to reverse a string or sequence of characters. This method can be used to reverse an integer by converting it to a string first, and then using the StringBuilder
class to reverse the string.
The reverse()
method takes no arguments and returns the reversed StringBuilder
object. To use this method, you first need to create an instance of the StringBuilder
class and pass the integer value to its constructor. Then, you can use the reverse()
method to reverse the string representation of the integer, and finally, convert it back to an integer using the parseInt()
method of the Integer
class.
Here's an example code snippet that demonstrates the usage of the reverse()
method to reverse an integer:
int num = 12345; StringBuilder sb = new StringBuilder(Integer.toString(num)); sb.reverse(); int reversedNum = Integer.parseInt(sb.toString()); System.out.println(reversedNum); // Output: 54321
In this code snippet, we first convert the integer num
to a string using the toString()
method of the Integer
class. Then, we create a new StringBuilder
instance and pass the string representation of num
to its constructor. Next, we call the reverse()
method of the StringBuilder
object to reverse the string. Finally, we convert the reversed string back to an integer using the parseInt()
method of the Integer
class and print the result.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.