Skip to Content

Java StringBuffer Reverse Example

  1. /*
  2.    Java StringBuffer Reverse Example
  3.    This example shows how to reverse the content of the StringBuffer using
  4.    reverse method of Java StringBuffer class.
  5. */
  6.  
  7. public class JavaStringBufferReverseExample {
  8.  
  9.   public static void main(String[] args) {
  10.    
  11.     //create StringBuffer object
  12.     StringBuffer sb = new StringBuffer("Java StringBuffer Reverse Example");
  13.     System.out.println("Original StringBuffer Content : " + sb);
  14.    
  15.     //To reverse the content of the StringBuffer use reverse method
  16.     sb.reverse();
  17.     System.out.println("Reversed StringBuffer Content : " + sb);
  18.   }
  19. }
  20.  
  21. /*
  22. Output Would be
  23. Original StringBuffer Content : Java StringBuffer Reverse Example
  24. Reversed StringBuffer Content : elpmaxE esreveR reffuBgnirtS avaJ
  25. */