Hi,
I recently started learning java language. Our sir had teach us String and StringBuffer but can't understand difference between them. So if you have any knowledge on String and StringBuffer please help me.
Hi,
I recently started learning java language. Our sir had teach us String and StringBuffer but can't understand difference between them. So if you have any knowledge on String and StringBuffer please help me.
Java language gives you two classes StringBuffer and String. The StringBuffer class is create to make any changes in character. String class is used to modified character string that unable to changed. In general StringBuffer class is faster than String class.
Eg. of string:
String S = new String ("Hello to all ");
S+= "How are you";
Eg. of StringBuffer:
StringBuffer S = new StringBuffer ("Welcome to my world ");
S.append("Techarena");
1.Objects of string class are immutable and constants while objects of StringBuffer class are not immutable and constants.
2.We use modifiable and growable string in StringBuffer Class where as constant strings are used in String class.
3.Once we create string then we can not modified them where as we can modified StingBuffer objects.
4.values of StringBuffer class are manipulated at compile time while values of String class are manipulated only at run time.
1.The main difference between String and stringbuffer is that stringbuffer object is thread safe while string is not.
2.In String we can not change content while in StringBuffer we can change content.
3.We can not create sub class of string where as we can create sub class of StringBuffer
4.In string we use compareTo() method to compare two string while in StringBuffer we don't have this method.
5.In string class we don't have reverse() method while in StringBuffer we have reverse() function.
Bookmarks