Why you should NOT concatenate the Strings using the '+' operator [Java]

preview_player
Показать описание
Why the simple Java string concatenation inside the for loop using the "+" operator is so slow? Why it's O(N^2) time complexity? Why StringBuilder is much faster compared to String in Java? What to use Java String or StringBuilder?

Want to find answers to these questions, check out this video in this case!

Thanks for watching!

#dataStructureAndAlgorithmsTipsAndTricks
#StringBuilderVsString
Рекомендации по теме
Комментарии
Автор

YouTube finally did a great recommendation!! Best edit and sounds effects are amazing!! thanks

perasperaadastra
Автор

Thank you so much for this. I was doing the Reverse Integer problem on Leetcode and decided to try to do it using string manipulation, and I was surprised how slow it was compared to other methods. If you don't mind me asking, why were strings designed this way? Are there any cases where Strings would be better than StringBuilders other than just convenience?

It seems to me that Strings should just act this way under the hood to be faster, but I assume I'm just not seeing the big picture. There has to be some reason Strings need to be immutable, right?

Turruc