Reverse String Program in Java W3Schools Guide

Robert Jul 13, 2026

Reversing a string in Java is a simple yet essential operation in programming, especially when dealing with inputs, outputs, or manipulating data. W3Schools, a popular learning resource for coding, offers a straightforward approach to reversing strings using Java. Let's dive into this basic yet powerful skill.

Java  Program to reverse every word in a String
Java Program to reverse every word in a String

Understanding string reversals is crucial as it helps in text manipulation, pattern matching, and can be a key part of more complex algorithms. So, how does one reverse a string in Java?

Java String Methods
Java String Methods

Basic String Reversal

Java provides a built-in class for strings, and it includes methods that simplify many string-related operations. To reverse a string, we'll utilize the StringBuilder class and its reverse method.

String Methods in Java Explained | Java Programming Basics
String Methods in Java Explained | Java Programming Basics

The StringBuilder class is used to create mutable sequences of characters. When you need to change the content of a string, you should use a StringBuilder instead of a String, as strings are immutable in Java.

Using StringBuilder to Reverse String

Strings in Java Explained | Java Programming Basics for Beginners
Strings in Java Explained | Java Programming Basics for Beginners

Here's a simple Java function that uses StringBuilder to reverse a string:

``` public class ReverseString { public static void main(String[] args) { String str = "Hello, World!"; String reversed = reverseString(str); System.out.println("Reversed: " + reversed); } public static String reverseString(String input) { StringBuilder sb = new StringBuilder(input); return sb.reverse().toString(); } } ```

In this code, the new StringBuilder(input) creates a new StringBuilder containing the input string, and the reverse() method then inverts the sequence of characters. The toString() method converts the result back to a string.

Reversing Strings Without StringBuilder

Java Bootcamp London & UK – Part Time Java Developer Program
Java Bootcamp London & UK – Part Time Java Developer Program

Although using StringBuilder is the most common way to reverse strings in Java, you can also achieve this without it. Here's an alternative method using loops:

``` public static String reverseString(String s) { String reverse = ""; for (int i = s.length() - 1; i >= 0; i--) { reverse = reverse + s.charAt(i); } return reverse; } ```

This function initializes an empty string, reverse, and then appends each character of the input string to it, starting from the last. The function does this by iterating through the string from the end to the beginning and using the charAt method to access each character.

Reversing Stringsline by Line

Java String equals() Method
Java String equals() Method

What if we need to reverse each line in a multi-line string? Well, we can use a combination of the methods above. Here's how:

First, we split the string into lines using the split method. We then reverse each line using the loop we've seen before, and finally, join them back together using the join method:

Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java
an image of a programming program with the words,'jwa program to compare two strings
an image of a programming program with the words,'jwa program to compare two strings
Java Patterns Notes 1
Java Patterns Notes 1
Java Cheatsheet
Java Cheatsheet
🚀 5 Essential Java Methods Every Developer Should Know! 💻
🚀 5 Essential Java Methods Every Developer Should Know! 💻
Java Strings - Learn the Essential Methods with its Syntax! - TechVidvan
Java Strings - Learn the Essential Methods with its Syntax! - TechVidvan
java programs
java programs
Develop
Develop
Java script string method
Java script string method
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Methods Of String Class In Java With Example
Methods Of String Class In Java With Example
Coding..
Coding..
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
a screen with the text 2 reverse an array description description description description description description description description description description description description description description description description description description description description description description description
Layout en Java. BorderLayout
Layout en Java. BorderLayout
Java Programming Essentials: A Guide to Mastery with Java Course in Pune
Java Programming Essentials: A Guide to Mastery with Java Course in Pune
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
[Solved] How to count Vowels and Consonants in Java String Word? Example
[Solved] How to count Vowels and Consonants in Java String Word? Example
🔴🔦 Master Java Loops in Minutes! 🚀🔦
🔴🔦 Master Java Loops in Minutes! 🚀🔦
a poster with different types of text and symbols on the back ground, including an image of
a poster with different types of text and symbols on the back ground, including an image of

``` public static String reverseLines(String s) { String[] lines = s.split("\\r?\\n"); String reverse = ""; for (String line : lines) { reverse = (reverse.isEmpty()) ? reverseString(line) : reverseString(reverse) + "\n" + reverseString(line); } return reverse; } ```

The split("\\r?\\n") part is used to handle both Windows ("\r\n") and Unix/Linux ("\n") line endings.

Reversing individual Words

And if you need to reverse each individual word in a string, you simply use the split method to divide the string into words, reverse each, and then join them together:

``` public static String reverseWords(String s) { String[] words = s.split(" "); String reverse = ""; for (String word : words) { reverse = (reverse.isEmpty()) ? reverseString(word) : reverseString(reverse) + " " + reverseString(word); } return reverse; } ```

Both of these examples can be handy when dealing with user input or manipulating text files.

Understanding and practicing these methods will provide you with a solid foundation in string manipulation in Java.

Keep practicing, keep exploring, and don't forget to check out W3Schools for more Java tutorials. Happy coding!