This document discusses arrays and StringBuffer class in Java. It defines single and multi-dimensional arrays and provides examples. It also explains the difference between String and StringBuffer classes and lists common methods of StringBuffer class like append(), insert(), reverse() etc. Examples are given to demonstrate the usage of these StringBuffer methods.
2. Array
Array is similar type of elements and share a common name.
Syntax
datatype variblename [ ] = new type [size of array]
Example
int a [ ] = new int [7]
Types of array :
1) Single dimension array
2) Multi dimension array
3. Single dimension array
Syntax
int a[ ] = new int [ 10 ]
An array using one subscript to represent the list of element is called
Single dimension array.
Multi dimension array
Syntax
int a [ ] = new int [3] [3]
row column
In Java , multi dimension array is actually array of arrays.
4. Stringclass
An obj of the string class represent a string of class.
String class belong the java.lang package which does not require the input-output
statement.
Like , other classes string has constructor and methods.
Syntax :
String s = new String (hello);
System.out.println(s);
String s1 = new String (s);
String s = hello world;
System.out.println(s);
5. StringBufferclass
The java.lang.StringBuffer class is a mutable sequence of characters.
A String buffer is like a string , but can be modified.
It contains some sequence of characters , but the length and content
of the sequence can be changed through certain method calls.
They are safe for by multiple threads.
Syntax
StringBuffer sb = new StringBuffer ();
6. DifferencebetweenStringandStringBuffer
BASIS FOR COMPARISON STRING STRINGBUFFER
Basic The length of the String object is
fixed.
The length of the StringBuffer can
be increased.
Modification String object is immutable. StringBuffer object is mutable.
Performance It is slower during concatenation. It is faster during concatenation.
Memory Consumes more memory. Consumes less memory.
7. Method of StringBuffer class
METHODS
Description
SetLength ( int size) It is used to set the length of string.
CharAt (int index) It returns the character at the specified index in a string.
SetcharAt (int index , char ch) It is used to set the specified character at the given
index. It replacing the old character at the given index.
append (String s) It is used to append the specified string with this String.
Insert (int index , String str) It is used to insert the specified String at the specified
position.
reverse( ) It is used to reverse the string.
replace (int startindex , int endindex ,String str) It is used to replace the string from specified startindex
and endindex.
8. getchars (int S , int E , char a[ ] , int
target)
Where , S = startindex
E = endindex
It is used for copying String characters to an array of
chars.
Ex :
StringBuffer sb = new StringBuffer (Java Program);
Char[ ] dst = new char[ ] {s , t , r , i , n , g , B ,
u , f , f , e , r };
Sb.getchars(5,12,dst,2);
System.out.println(dst); //stProgramer
9. ExampleofmethodStringBufferclass
StringBuffer sb = new StringBuffer (VIE);
sb.SetLength (2); //VI
char c = sb.charAt (1);
System.out.println(c); // V
sb.SetcharAt ( 0 , A);
System.out.println(sb.toString()); //AIE
String str = college;
sb.append(str);
System.out.println(sb.toString()); //VIE college
sb.insert (1 , kotambi);
System.out.println(sb.toString()); //VkotambiIE