Using String Reverse in Java

Reversing a string is a common and often encountered task in the expansive world of coding. Java, recognized as one of the leading programming languages, provides multiple solutions to tackle this problem. Among the myriad ways to invert a string, one approach shines due to its straightforwardness and efficiency: leveraging the inherent functions of the StringBuilder (or its similar counterpart, StringBuffer) class. In contrast to some languages where inverting a string demands loops or tailor-made functions, Java's StringBuilder class comes with a built-in method conveniently named `reverse`. This function effortlessly completes the job, making the act of string inversion almost a breeze for coders. In this Java Reverse String Example, we reverse a string using the Java StringBuilder class. Click Execute to run the Java Reverse String Example online and see the result.
Using String Reverse in Java Execute
public class StringReverseExample {

    public static void main(String[] args) {
        String original = "JavaProgramming";
        String reversed = new StringBuilder(original).reverse().toString();
        System.out.println("Original String: " + original);
        System.out.println("Reversed String: " + reversed);
Updated: Viewed: 41 times