In this article, we will learn what is different between String ,StringBuffer and StringBuilder. Let’s keep this article small and simple jumping into the topic → These three are pre-fined final class in java.
- String is a predefined final class present in the java.lang package and introduced from JDK version 1.0.
- String is immutable in nature.
- String is Thread safe.
- String class has overridden three method from object class
- toString()
- hashcode()
- equals()
- String objects can be created using new and without new keyword (literal)
- String (+) operator can be used for the concatenation.
- StringBuffer is a predefined final class present in the java.lang package and introduced from JDK version 1.0.
- StringBuffer is mutable in nature.
- StringBuffer is Thread safe.
- In StringBuffer only the toString() method is overridden from the Object class.
- StringBuffer objects can be created using the new keyword,we can’t use the literal method.
- In StringBuffer we can’t use the (+) for the concatenation ,instead of that we should use the concat() method.
- StringBuilder is a predefined final class present in the java.lang package and introduced from JDK version 1.5.
- StringBuilder is mutable in nature.
- StringBuilder is not Thread safe.
- In StringBuilder only the toString() method is overridden from the Object class.
- As same as StringBuffer ,StringBuilder objects can be created using the new keyword,we can’t use the literal method (without the new keyword).
- IN StringBuilder we can’t use the (+) for the concatenation ,instead of that we should use the concat() method.
That’s all for this article. Hope you have enjoyed this article :)