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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:07:21+00:00 2026-05-11T03:07:21+00:00

We have to build Strings all the time for log output and so on.

  • 0

We have to build Strings all the time for log output and so on. Over the JDK versions we have learned when to use StringBuffer (many appends, thread safe) and StringBuilder (many appends, non-thread-safe).

What’s the advice on using String.format()? Is it efficient, or are we forced to stick with concatenation for one-liners where performance is important?

e.g. ugly old style,

String s = 'What do you get if you multiply ' + varSix + ' by ' + varNine + '?'; 

vs. tidy new style (String.format, which is possibly slower),

String s = String.format('What do you get if you multiply %d by %d?', varSix, varNine); 

Note: my specific use case is the hundreds of ‘one-liner’ log strings throughout my code. They don’t involve a loop, so StringBuilder is too heavyweight. I’m interested in String.format() specifically.

  • 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. 2026-05-11T03:07:22+00:00Added an answer on May 11, 2026 at 3:07 am

    I wrote a small class to test which has the better performance of the two and + comes ahead of format. by a factor of 5 to 6. Try it your self

    import java.io.*; import java.util.Date;  public class StringTest{      public static void main( String[] args ){     int i = 0;     long prev_time = System.currentTimeMillis();     long time;      for( i = 0; i< 100000; i++){         String s = 'Blah' + i + 'Blah';     }     time = System.currentTimeMillis() - prev_time;      System.out.println('Time after for loop ' + time);      prev_time = System.currentTimeMillis();     for( i = 0; i<100000; i++){         String s = String.format('Blah %d Blah', i);     }     time = System.currentTimeMillis() - prev_time;     System.out.println('Time after for loop ' + time);      } } 

    Running the above for different N shows that both behave linearly, but String.format is 5-30 times slower.

    The reason is that in the current implementation String.format first parses the input with regular expressions and then fills in the parameters. Concatenation with plus, on the other hand, gets optimized by javac (not by the JIT) and uses StringBuilder.append directly.

    Runtime comparison

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

Sidebar

Related Questions

I have a custom class that I use to build table strings. I would
I'm attempting to build a method call from strings that have been passed into
I have an array of NSDates which I build from strings using [NSDate dateFromString]
Does Python have a built-in, simple way of encoding/decoding strings using a password? Something
I have a need to build the data string dynamically. This is not working,
(java 1.5) I have a need to build up a String, in pieces. I'm
I have a javascript question. I'm trying to dynamically build a plugins string variable
i have build a small function whose work to get value in alert box
I have a template for an email that I've put in a localized strings
I have build a custom Html Helper extension as follows: public static string DatePicker(this

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.