Is there a performance overhead in executing sb.indexOf(c + "")
where c is of type Character or char and sb is StringBuilder object?
Is there a performance overhead in executing sb.indexOf(c + ) where c is of
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
You can use
String.valueOfThere are good things about this approch.
char data[] = {c};so no additional operation is required.2is really a micro optimization and I would always go for option1i.e. “clean code”.For what it’s worth, here’s the bytecode generated by the concatenation version:
As you can see, it creates a second
StringBuilder, does twoappendcalls, and then atoString. In contrast, here’s theString.valueOfversion:That just hands the
Character(which has already been automatically unboxed into achar) off toString.valueOf. So what does that do? Let’s look at the JDK source code:So it creates a new one-character array and hands off directly to the
Stringconstructor. Very likely to be more efficient.But again, it’s probably a micro-optimization. The
String.valueOfcall makes the code clearer, which is the main thing.