🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the StringBuilder reverse() method in Java programming and how to use it with an example.
1. StringBuilder reverse() Method Overview
Definition:
The reverse() method of the StringBuilder class is used to reverse the contents of the sequence, making it possible to easily invert the characters in a string.
Syntax:
- `stringBuilder.reverse()`
Parameters:
- The method doesn't take any parameters.
Key Points:
- After executing the reverse() method, the first character of the original sequence becomes the last in the modified sequence, the second becomes the second last, and so on.
- This method is particularly useful when you need to find a reversed version of a string, like in palindrome checking algorithms.
- Since StringBuilder is mutable, this operation modifies the content of the StringBuilder object without creating a new instance. This makes the operation faster and more memory efficient compared to string concatenations.
2. StringBuilder reverse() Method Example
public class StringBuilderReverseExample {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder("Java is fun!");
// Reversing the StringBuilder content
builder.reverse();
System.out.println(builder); // Outputs: !nuf si avaJ
// Using reversed string in a real-world example: palindrome check
StringBuilder palindromeCandidate = new StringBuilder("level");
StringBuilder original = new StringBuilder(palindromeCandidate);
palindromeCandidate.reverse();
if (original.toString().equals(palindromeCandidate.toString())) {
System.out.println(original + " is a palindrome.");
} else {
System.out.println(original + " is not a palindrome.");
}
}
}
Output:
!nuf si avaJ level is a palindrome.
Explanation:
In this example:
1. We start with a StringBuilder object containing the string "Java is fun!".
2. By using the reverse() method, we modify the content of the StringBuilder to its reversed version, which is "!nuf si avaJ".
3. Next, we demonstrate a real-world use-case where the reverse() method can be handy. We check if a string is a palindrome by comparing it with its reversed version. In our case, "level" is indeed a palindrome.
The reverse() method in StringBuilder allows for quick in-place reversal of strings, enhancing performance and offering utility in various string manipulation scenarios.
Related Java StringBuilder class method examples
- Java StringBuilder append() example
- Java StringBuilder delete() example
- Java StringBuilder insert() example
- Java StringBuilder reverse() example
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment