Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 17
String is immutable - it cannot change its internal state. Each method you invoke on a String creates a new object and returns it. For the reason, String does not allow appending. On the other hand, StringBuilder is mutable - When you call append(..) it alters the internal char array, rather than creating a new String object. Let us take this code as an example : StringBuilder str = new StringBuilder(); for (int i = 0; i < 50; i ++) { str.append(i); } // This appends value of i to str. This is not possible with a String object!
17th Oct 2017, 4:53 AM
Dev
Dev - avatar