Java Online Compiler
Example: Reverse String using StringBuilder in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Reverse String using StringBuilder public class Main { public static void main(String[] args) { // Step 1: Define the original string String originalString = "programming"; // Step 2: Create a StringBuilder object from the original string StringBuilder stringBuilder = new StringBuilder(originalString); // Step 3: Use the reverse() method to reverse the StringBuilder content stringBuilder.reverse(); // Step 4: Convert the reversed StringBuilder back to a String String reversedString = stringBuilder.toString(); // Step 5: Print the original and reversed strings System.out.println("Original String: " + originalString); System.out.println("Reversed String: " + reversedString); } }
Output
Clear
ADVERTISEMENTS