Category:
JAVA STRING INTERVIEW QUESTIONS AND ANSWERS
You should avoid using the + operator for string concatenation inside loops because it triggers excessive memory consumption and quadratic O(n^2) time complexity. As Strings are immutable , they cannot be modified in place . Every single + operation copies the entire existing string into a brand-new memory location .
Instead of using + in a loop , StringBuilder offers a more efficient way to build strings . It doesn’t create a new object for each concatenation , which helps in reducing memory allocations and improve overall performance.
