public StringBuilder reverse()This method returns an instance of StringBuilder. The interesting thing is, the returned value is actually itself. So what this method does is, it reverses itself and returns itself. This is the source code for StringBuilder.reverse:
public StringBuilder reverse() {In the following example, the old and the reversed string will print the same content after calling reverse method:
super.reverse();
return this;
}
public static void main(String[] args) {If you still need to access the original content of the StringBuilder, you will need to save it to string before invoking sb.reverse().
StringBuilder old = new StringBuilder(args[0]);
StringBuilder newsb = old.reverse();
System.out.println("original string: " + old);
System.out.println("new string: " + newsb);
}
Tags: