Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 962261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:28:43+00:00 2026-05-16T01:28:43+00:00

i am trying to do a simple string manipulation. input is murder, i want

  • 0

i am trying to do a simple string manipulation. input is “murder”, i want to get “murderredrum”.

i tried this

String str = "murder";
StringBuffer buf = new StringBuffer(str);
// buf is now "murder", so i append the reverse which is "redrum"
buf.append(buf.reverse());
System.out.println(buf);

but now i get “redrumredrum” instead of “murderredrum”.

can someone explain what’s wrong with my program? thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:28:43+00:00Added an answer on May 16, 2026 at 1:28 am

    The short answer

    The line:

    buf.append(buf.reverse());
    

    essentially does the following:

    buf.reverse();    // buf is now "redrum"
    buf.append(buf);
    

    This is why you get "redrumredrum".

    That is, buf.reverse() doesn’t return a new StringBuffer which is the reverse of buf. It returns buf, after it had reversed itself!

    There are many ways to "fix" this, but the easiest would be to explicitly create a new StringBuffer for the reversal, so something like this:

    buf.append(new StringBuffer(str).reverse());
    

    Deeper insight: comparing String and StringBuffer

    String in Java is immutable. On the other hand, StringBuffer is mutable (which is why you can, among other things, append things to it).

    This is why with String, a transforming method really returns a new String. This is why something like this is "wrong"

    String str = "murder";
    str.toUpperCase(); // this is "wrong"!!!
    System.out.println(str); // still "murder"
    

    Instead you want to do:

    String str = "murder";
    str = str.toUpperCase(); // YES!!!
    System.out.println(str); // now "MURDER"!!!
    

    However, the situation is far from analogous with StringBuffer. Most StringBuffer methods do return StringBuffer, but they return the same instance that it was invoked on! They do NOT return a new StringBuffer instance. In fact, you’re free to discard the "result", because these methods have already accomplished what they do through various mutations (i.e. side effects) to the instance it’s invoked upon.

    These methods could’ve been declared as void, but the reason why they essentially return this; instead is because it facilitates method chaining, allowing you to write something like:

    sb.append(thisThing).append(thatThing).append(oneMoreForGoodMeasure);
    

    Related questions

    • Method chaining – why is it a good practice, or not?
    • Fluent Interfaces – Method Chaining

    Appendix: StringBuffer vs StringBuilder

    Instead of StringBuffer, you should generally prefer StringBuilder, which is faster because it’s not synchronized. Most of the discussions above also applies to StringBuilder.

    From the documentation:

    StringBuffer : A thread-safe, mutable sequence of characters. […] As of JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder, which should generally be preferred as it supports all of the same operations but faster, as it performs no synchronization.

    StringBuilder : A mutable sequence of characters. […] Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

    Related questions

    • StringBuilder and StringBuffer in Java

    Bonus material! Alternative solution!

    Here’s an alternative "fix" to the problem that is perhaps more readable:

    StringBuilder word = new StringBuilder("murder");
    StringBuilder worddrow = new StringBuilder(); // starts empty
    
    worddrow.append(word).append(word.reverse());
    
    System.out.println(worddrow); // "murderredrum"
    

    Note that while this should do fine for short strings, it does use an extra buffer which means that it’s not the most efficient way to solve the problem.

    Related questions

    • Reverse a string in Java, in O(1)? – as a CharSequence, yes this can be done!

    Bonus material again! The last laugh!

    StringBuilder sb = new StringBuilder("ha");
    sb.append(sb.append(sb));
    System.out.println(sb); // "hahahaha"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 523k
  • Answers 523k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It seems at your code that you are generating li… May 16, 2026 at 9:42 pm
  • Editorial Team
    Editorial Team added an answer As I see it you have three options: You relate… May 16, 2026 at 9:42 pm
  • Editorial Team
    Editorial Team added an answer 600, 400 and 200, respectively "slow", "normal", "fast". All in… May 16, 2026 at 9:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to do some simple string manipulation with the href attribute of a
I'm trying some string manipulation in my web app, but I don't want to
Hi i am trying to execute this simple HQL query to get a list
I am trying to do a simple encryption of a string and am getting
I'm trying to do a simple custom runtime_error. I define the class: #include <string>
I'm trying to make a simple app with glade/gtk/vala. So far I have this:
I'm trying to write a program that takes input of - hexadecimals, octals, and
I am trying to query against an IList<string> property on one of my domain
As an exercise, I'm trying to create a input stream manipulator that will suck
Is there a way to use the rails match method with a simple string,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.