Search code examples
javaperformancestringcomplexity-theorystringbuilder

Java CharAt() and deleteCharAt() performance


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 ?


Solution

  • 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.