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 8981563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:22:04+00:00 2026-06-15T20:22:04+00:00

What should be used for a basic string concatenation operation ? String randomString =

  • 0

What should be used for a basic string concatenation operation ?

String randomString = "hello " + userName + " how are you" ?

or

String randomString = String.format("hello %s how are you ?",userName);

I feel String.format() gives a better idea of output string.
But what are actually pro and cons in using any of one ?

Is there anything in terms of performance or orphan entries in string literal pool.

Edit : I am talking about multiple parameters in string, not just one. Around 5 +.

Bonus Question : Please also share your view on
which one should actually use ? any one of these or 3rd one… !! ?

  • 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-15T20:22:05+00:00Added an answer on June 15, 2026 at 8:22 pm

    If you are looking for performance only I believe that using StringBuilder/StringBuffer is the most efficient way to build strings. Even if the Java compiler is smart enough to translate most of String concatenations to StringBuilder equivalent.

    If you are looking for readability the String.format thing is the much clearer I think, and this is what I use also unless I need to rely on high performance.

    So if your main concern is not performance, meaning this code is not in a path that is called a lot, you may prefer to use String.format as it gives a better idea of the resulting String (like you said).

    Besides, using String.format lets you use the format thing, which means you can use it for padding Strings, formatting numbers, dates, and so on, which would make the code even worse if using simple concatenation.

    Edit for Chuu:

    Using JAD, you can see that the following code:

    public class Test {
        public static void main(String[] args) {
            String str = "a" + "b" + "c";
            String str2 = "foo" + str + "bar" + str;
            System.out.println(str2);
        }
    }
    

    when decompiled will look like:

    public class Test {
        public static void main(String[] args) {
            String str = "abc";
            String str2 = new StringBuilder("foo").append(str).append("bar").append(str).toString();
            System.out.println(str2);
        }
    }
    

    Proof of that can also be found using the javap utility that will show you the Java bytecode under a .class file:

    public static void main(java.lang.String[] args);
        0  ldc <String "abc"> [16]
        2  astore_1 [str]
        3  new java.lang.StringBuilder [18]
        6  dup
        7  ldc <String "foo"> [20]
        9  invokespecial java.lang.StringBuilder(java.lang.String) [22]
       12  aload_1 [str]
       13  invokevirtual java.lang.StringBuilder.append(java.lang.String) : java.lang.StringBuilder [25]
       16  ldc <String "bar"> [29]
       18  invokevirtual java.lang.StringBuilder.append(java.lang.String) : java.lang.StringBuilder [25]
       21  aload_1 [str]
       22  invokevirtual java.lang.StringBuilder.append(java.lang.String) : java.lang.StringBuilder [25]
       25  invokevirtual java.lang.StringBuilder.toString() : java.lang.String [31]
       28  astore_2 [str2]
       29  getstatic java.lang.System.out : java.io.PrintStream [35]
       32  aload_2 [str2]
       33  invokevirtual java.io.PrintStream.println(java.lang.String) : void [41]
       36  return
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I know the basics about when indexing should be used and the pros
html() should be used for setting content in non-form elements such as div's. **
which one should be used to manipulating data, Array or Array Object? Like search,sort
as we No keyword should be used as an Identifier in java. But there
Does anyone know when XML should be used instead of JSON and why? Thanks
I think I may have used a repeater when I should have used something
I'm writing a little library that should be used by people who don't know
AFAIK, extern keyword should be used for declaration and no value can be associated
There are a lot of blogs saying that a hasOwnProperty check should be used
In vim , what command should be used if I wanna join next line

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.