Java CharAt() and deleteCharAt() performance

Jimmar picture Jimmar · Jun 24, 2011 · Viewed 33.8k times · Source

I've been wondering about the implementation of charAt function for String/StringBuilder/StringBuffer in java what is the complexity of that ? also what about the deleteCharAt() in StringBuffer/StringBuilder ?

Answer

Matt Ball picture Matt Ball · Jun 24, 2011

For String, StringBuffer, and StringBuilder, charAt() is a constant-time operation.

For StringBuffer and StringBuilder, deleteCharAt() is a linear-time operation.

StringBuffer and StringBuilder have very similar performance characteristics. The primary difference is that the former is synchronized (so is thread-safe) while the latter is not.