Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Presentation 2
Objectives
At the end of this presentation, you will be able to: Differentiate between String and StringBuffer class
length() method
Finds the current length of a StringBuffer object.
length() method
Example
12
capacity() method
Returns the amount of memory allocated for the StringBuffer object.
capacity() method
Example
30
Hands-On!
Program funcdemo1.java will illustrate the use of length() and capacity() methods.
append() method
Allows data to be added to the end of a StringBuffer object.
append() method
Example
Welcome to beautiful_World
setCharAt() method
Sets the character at any position within a StringBuffer.
setCharAt() method
Example
Hello_World
insert() method
Allows data to be added at a specified position in the StringBuffer object.
insert() method
Example
Hands-On!
Program funcdemo2.java will illustrate the use of append() and insert() methods.
reverse() method
Reverses the order of characters in a StringBuffer object.
reverse() method
Example
Tower
Hands-On!
Program ReverseItAll.java will illustrate the use of reverse() method.
Lab Exercise
3. Create a StringBuffer object and illustrate how to append characters. Display the capacity and length of the StringBuffer object. 4. Create a StringBuffer object and illustrate how to insert characters at its beginning. 5. Create a StringBuffer object and illustrate the operation of the reverse() method.
Summary
In this session, you learnt the following: StringBuffer will automatically grow to make room for additions and insertions. Characters can be inserted or appended to a StringBuffer object using its methods.
Assignment
1.
2.