Let’s first understand how String class works: This statement is equivalent to the object creation of String class in the following way: The append operation on strings is often required while building a string. Generally, programmers use concatenation operator ( + ) to join two strings, for example: But the instances of String class are immutable (i.e. unchangeable, read-only) -they cannot be changed once created. So, to implement the string concatenation operator, Java uses the StringBuffer class (which is mutable). Internally the above statement is implemented as: It is clear that while building large strings it is better to use StringBuffer class and its methods (like append) instead of using String class and + operator. StringBuffer code will execute faster and will take lesser memory. I hope this tiny tip was useful for you and will save time for you and memory for your Java programs! Comment * Name * Email * Website
Δ