java

To Reverse A String: StringBuffer in Java

Java provides the StringBuffer and String classes. The String class is used to manipulate character strings that cannot be changed. To put it simply: objects of type String are read only and immutable.

The StringBuffer class is used to represent characters that cannot be modified.

The significant performance between these two classes is that the StringBuffer is remarkably faster than the String when performing simple concatenations.

Using the String class, we can concatenate as follows:

String str = new String("Lawson ");
str += "Luke";

With String Buffer

StringBuffer strBuff = new StringBuffer("Lawson ");
strBuff.append("Luke");

To reverse a String …

public class StringReverse{
public static void main(String args[]){
String str = new String("Luke");
StringBuffer buff = new StringBuffer(str);
System.out.println(buff.reverse());
}
}
Share
Published by
codeflare

Recent Posts

How to Debug Your JavaScript Code

Debugging JavaScript code can sometimes be challenging, but with the right practices and tools, you…

8 hours ago

Service Workers in JavaScript: An In-Depth Guide

Service Workers are one of the core features of modern web applications, offering powerful capabilities…

2 weeks ago

What are Database Driven Websites?

A database-driven website is a dynamic site that utilizes a database to store and manage…

2 weeks ago

How to show Toast Messages in React

Toasts are user interface elements commonly used in software applications, especially in mobile app development…

2 weeks ago

Exploring the Relationship Between JavaScript and Node.js

JavaScript has long been synonymous with frontend web development, powering interactive and dynamic user interfaces…

3 weeks ago

Key Differences Between Tailwind CSS and CSS3

Introduction: In the world of web development, CSS (Cascading Style Sheets) plays a crucial role…

4 weeks ago