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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:14:01+00:00 2026-06-13T05:14:01+00:00

Is there a performance overhead in executing sb.indexOf(c + ) where c is of

  • 0

Is there a performance overhead in executing sb.indexOf(c + "")
where c is of type Character or char and sb is StringBuilder object?

  • 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-13T05:14:02+00:00Added an answer on June 13, 2026 at 5:14 am

    You can use String.valueOf

    builder.indexOf(String.valueOf(c));
    

    There are good things about this approch.

    1. Clean code
    2. String.valueOf creates String object using char[] from passed char likechar data[] = {c}; so no additional operation is required.

    2 is really a micro optimization and I would always go for option 1 i.e. “clean code”.

    For what it’s worth, here’s the bytecode generated by the concatenation version:

    new #2; //class java/lang/StringBuilder
    dup
    invokespecial #6; //Method java/lang/StringBuilder."<init>":()V
    aload_1
    invokevirtual #7; //Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
    ldc #8; //String 
    invokevirtual #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
    invokevirtual #10; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
    invokevirtual #11; //Method java/lang/StringBuilder.indexOf:(Ljava/lang/String;)I
    

    As you can see, it creates a second StringBuilder, does two append calls, and then a toString. In contrast, here’s the String.valueOf version:

    aload_0
    aload_1
    invokestatic #12; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
    invokevirtual #11; //Method java/lang/StringBuilder.indexOf:(Ljava/lang/String;)I
    

    That just hands the Character (which has already been automatically unboxed into a char) off to String.valueOf. So what does that do? Let’s look at the JDK source code:

    public static String valueOf(char c) {
        char data[] = {c};
        return new String(0, 1, data);
    }
    

    So it creates a new one-character array and hands off directly to the String constructor. Very likely to be more efficient.

    But again, it’s probably a micro-optimization. The String.valueOf call makes the code clearer, which is the main thing.

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

Sidebar

Related Questions

1.. Is there any performance overhead caused by the usage of attributes? Think for
I am wondering if there are any performance overhead issues to consider when using
Is there any overhead using partial classes in case of memory, performance etc? If
Are there any performance/other implications in having an object relates to itself? (self join)
Is there any performance overhead in using RaiseEvent in .NET ? I have a
Is there any performance overhead that you take on when using Structs (as compared
Is there any performance or memory overhead using UIImage or UILabel or NSString in
Does anyone know if there are studies done that show the performance overhead of
I know there is a performance overhead for loading the font file, but the
Operator delete checks itself if the pointer is nullptr. Is there any performance overhead

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.