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

  • Home
  • SEARCH
  • 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 8054527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:10:11+00:00 2026-06-05T08:10:11+00:00

Possible Duplicate: when to use StringBuilder in java If not which of these pieces

  • 0

Possible Duplicate:
when to use StringBuilder in java

If not which of these pieces of code is better and why

public String backAround(String str) {
   int len = str.length();
   StringBuilder sb = new StringBuilder(str);
   char chEnd = sb.charAt(len-1);
  if(len > 0){
  sb = sb.append(chEnd);
  sb= sb.insert(0,chEnd);

  str= sb.toString();
  return str;

  }else{ return str;}
}

or

public String backAround(String str) {
  // Get the last char
  String back = str.substring(str.length()-1, str.length());
  return back + str + back;
}
  • 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-06-05T08:10:12+00:00Added an answer on June 5, 2026 at 8:10 am

    If you are just “sticking a few elements together” as in your backAround() method, you may as well just use the + notation. The compiler will convert this into appropriate StringBuilder.append()s for you, so why bother ‘spelling things out’.

    The idea of explicitly using StringBuilder is that in principle you can hand-optimise how exactly the elements are appended to the string, including setting the initial buffer capacity and ensuring that you don’t accidentally create intermediate String objects that are unnecessary in cases where the compiler might not predict these things.

    So essentially, explicitly use a StringBuilder when there is slightly more complex logic to deciding what to append to the string. For example, if you are appending things in a loop, or where what is appended depends on various conditions at different points. Another case where you might use StringBuilder is if the string needs to be built up from various methods, for example: you can then pass the StringBuilder into the different methods and ask them to append the various elements.

    P.S. I should say that StringBuilder buys you a little more editing power as well (e.g. among other things, you can set its length) and, given the presence of the Appendable interface, you can actually create more generic methods that either append to a StringBuilder or to e.g. a StringWriter. But these are marginal cases, I would submit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: java String concatenation StringBuilder vs String concatenation in toString() in Java I
Possible Duplicate: String vs StringBuilder i know .NET Strings are immutable which is the
Possible Duplicate: Use of var keyword in C# Which one is better? using var
Possible Duplicate: Why use getters and setters? I have read books on Java, saying
Possible Duplicate: When to use StringBuilder? I am working hard in performance gaining in
Possible Duplicate: Use of class definitions inside a method in Java Can we have
Possible Duplicate: Why use iterators instead of array indices? string::iterator it; for (it =
Possible Duplicate: Weird use of void I was reading C code and came across
Possible Duplicate: Why use partial classes? Which way is best practice and the best
Possible Duplicate: String vs StringBuilder I just revisited some of the books that I

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.