In this topic, you will learn about, String and String Buffer classes.
String Classes: This class allows storing a collection of letters, numbers,s, or symbols by using the dynamic concepts. String class uses java.lang package and it also offers various pre-defined functions. By-default string class is initialized with a null value and we can use different constructors for string initialization.
To create a string:
String <string object>;
<string object>=new string(argument);
Eg:-
- string obj; Obj=new string(’’hello!’’);
- string str; str=new string(10);
- String str; Str=newString();
Buffer string Class: String buffer class is a peer(related) class of string but it offers facility to create variable size memory.so,we can easily increase or decrease memory size . String class also offers facility to insert element at given position and delete element from given position.
To create string Buffer:
Syntax:
stringBuffer <object>;
<object>=new <stringBuffer(’’string’’)>;
Eg:
- StringBuffer str;
Str=newStringBuffer(10); - StringBuffer str;
str=newstringBuffer(’’string’’); - StringBuffer str;
str=newStringBuffer();
Difference Between String and String Buffer classes:
String Buffer classes | String classes |
String buffer class represent same as string but create fix size string object. | String class create dynamic memory but we cannot create fix size string object |
A string buffer object is of flexible size or content. | String class contents and size cannot change if fix size and content defined. |
String buffer offers a facility to insert substring at any position. | A string cannot insert any element in the middle of the string. |
String buffer allows appending given string at the end. | String class object size cannot increase or decrease, so, we cant append. |
String buffer uses java. util package for declaration. | String class uses i.o and lang for declaration. |
String buffer class object provides a set of functions like append, insert, setlength,setcharacter, etc. | String class provides manipulation function only like length, char At and short. |
Comment below if you have queries related to the above topic, String and String Buffer classes.