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 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
Tag: stringbuilder
String, StringBuffer, and StringBuilder
Please tell me a real time situation to compare String, StringBuffer, and StringBuilder? Answer Mutability Difference: String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values. Thread-Safety Difference: The difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe. So when the application needs
Dumping a Java StringBuilder to File
What is the most efficient/elegant way to dump a StringBuilder to a text file? You can do: But is this efficient for a very long file? Is there a better way? Answer As pointed out by others, use a Writer, and use a BufferedWriter, but then don’t call writer.write(stringBuilder.toString()); instead just writer.append(stringBuilder);. EDIT: But, I see that you accepted a