String, Stringbuffer and Stringbuilder difference in java

preview_player
Показать описание
Difference Between String, Stringbuffer and Stringbuilder
String :
1.String class is immutable.
2.String is slow and consumes more memory when you concat too many strings because every time it creates new instance.
3.String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.

Stringbuffer :
1.StringBuffer class is mutable.
2.StringBuffer is fast and consumes less memory when you cancat strings
3.StringBuffer class doesn't override the equals() method of Object class.
4.Synchronized

Stringbuilder :
1.StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously.
2. StringBuilder is more efficient than StringBuffer.

Where to use String, StringBuffer and StringBuilder
String : If our content is fixed and not changing frequently then we should go for String

String Buffer: If our content is not fixed as well as thread safety is required then we should go for String buffer.

String Builder: If our content is not fixed, changed frequently and thread safety is not required then we should go for String Builder.

Please Like, Comment and Subscribe for more videos.

Know About the JVM Architecture

Please Like, Comment and Subscribe for more videos.
Рекомендации по теме