0

I would like to know about the relation between string literal and java performance.

for example, uses of below statements number of times makes any impact in performance.I have thousands of classes and many time we are using below statements :

1) buffer.append(",");
2) buffer.append("}");

3)String.append("10,000 times...same lines") // printing same lines in many classes
3)String.someStringMethod("same line many times") // using any String method 

Does this cause performance impact in terms of memory management etc.Do we have any cleaner way ?

Thanks

5
  • 2
    This is (mostly) not valid Java code, so it is difficult to comment on its efficiency. Commented Jan 26, 2013 at 9:34
  • @Stephen : yeah its not valid.i was just trying to give an example... Commented Jan 26, 2013 at 9:36
  • 1
    Well my point is that it is not usefule to comment on an example that makes no sense. Please provide meaningful examples of the kind of code you want us to comment on. (Your updates haven't helped ...) Commented Jan 26, 2013 at 9:37
  • Use a memory profiler and find out for sure. Commented Jan 26, 2013 at 9:39
  • @Stephen : I understand.Let me try to make to more clear. I have 1000's of classes in which we are using such type of code.for example : many times buffer.append(",")...same comma..or can say same string Commented Jan 26, 2013 at 9:40

2 Answers 2

3

It is really difficult to comment on examples that make no sense. However:

  • In general there are no particular efficiency concerns with Java String literals.
  • In general there are no particular efficiency concerns with methods that take String literals as arguments.
  • String concatenation / building can present efficiency concerns if a particular piece of code is executed often enough. However, if you need to build strings, then there is not a lot you can do about it.

There are one or two things that it is worth taking steps to avoid. The main one is this:

   String s = "";
   for (/* lots of times */) {
       // do stuff
       s += someOtherString;
   }

The problem is that this generates and then discards lots of temporary Strings. The more efficient way to do it is this:

   StringBuilder sb = new StringBuilder();
   for (/* lots of times */) {
       // do stuff
       sb.append(someOtherString);
   }
   String s = sb.toString();

However, it is probably only worth while optimizing this kind of thing if the profiler tells you that this particular bit of code is a bottleneck.

Sign up to request clarification or add additional context in comments.

Comments

1
  1. Any code you write affects performance, so it's always better to invoke one append() with ",}" instead of 2 appends.

  2. There's no method append in java.lang.String class.

  3. None of the String methods make changes to the String object. Instead, methods like String.substring(), String.concat(), String.replace() create new String objects. This means performance is affected more significantly than if you use StringBuffer.

  4. So generally StringBuffer methods are faster than those of String. However, new class was recently introduced called StringBuilder. There's only one difference from StringBuilder - it's not thread-safe. In real world cases thread management is taken care of higher level containers thus making it unnecessary to ensure thread safety for each class. In those cases you're advised to use StringBuilder. That should be the fastest.

  5. In order to further improve performance of StringBuilder you have to be aware of resulting string length to allocate StringBuilder of a proper size. If it's too big you'll waste some memory, but that's usually a minor problem. If it's too small though, StringBuilder will have to recreate internal character array to make space for more characters. That would make that particular append() invocation slow. Actually, that's not the invocation that's slow, but garbage collection invoked to clean the memory up.

  6. Particular methods of String class may be better or faster than those in StringBuffer/StringBuilder, but you have to be more specific with your questions for me to answer that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.