OBJECT-ORIENTED
PROGRAMMING
StringBuffer – StringBuilder -
StringTokenizer
Outline
StringBuffer
StringBuilder
String and StringBuffer
StringBuffer and StringBuilder
StringTokenizer
[501043 Lecture 1: Intro to Java] 2
StringBuffer
Java StringBuffer class is used to create
mutable (modifiable) string. The StringBuffer
class in java is same as String class except it
is mutable i.e. it can be changed.
Java StringBuffer class is thread-safe i.e.
multiple threads cannot access it
simultaneously. So it is safe and will result in
an order
[501043 Lecture 1: Intro to Java] 3
StringBuffer
Constructor:
[501043 Lecture 1: Intro to Java] 4
StringBuffer
Important methods
[501043 Lecture 1: Intro to Java] 5
StringBuffer
Important methods
[501043 Lecture 1: Intro to Java] 6
StringBuffer
append()
class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
[Link]("Java");//now original string is changed
[Link](sb);//prints Hello Java
}
}
[501043 Lecture 1: Intro to Java] 7
StringBuffer
insert()
class StringBufferExample2{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
[Link](1,"Java");//now original string is changed
[Link](sb);//prints HJavaello
}
}
[501043 Lecture 1: Intro to Java] 8
StringBuffer
replace()
class StringBufferExample3{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
[Link](1,3,"Java");
[Link](sb);//prints HJavalo
}
}
[501043 Lecture 1: Intro to Java] 9
StringBuffer
delete()
class StringBufferExample4{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
[Link](1,3);
[Link](sb);//prints Hlo
}
}
[501043 Lecture 1: Intro to Java] 10
StringBuffer
reverse()
class StringBufferExample5{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
[Link]();
[Link](sb);//prints olleH
}
}
[501043 Lecture 1: Intro to Java] 11
StringBuilder
Java StringBuilder class is used to create
mutable (modifiable) string. The Java
StringBuilder class is same as StringBuffer
class except that it is non-synchronized. It is
available since JDK 1.5.
[501043 Lecture 1: Intro to Java] 12
StringBuilder
[501043 Lecture 1: Intro to Java] 13
StringBuilder
The examples of StringBuilder class are
similar to StringBuffer’s.
[501043 Lecture 1: Intro to Java] 14
Exercises
Use StringBuffer, StringBuilder and
StringTokenizer to complete these requirement
Give a Vietnamese fullname (ex “Nguyen Van Teo”).
Students need to write methods as follows:
1. Count how many words in the name.
2. Return a first name
3. Return a last name
4. Return a middle name
5. Capitalize the first character in each word of the name
6. Formailize the name, including:
Delete spaces in front and behind of the name.
Leave one space between the words of the name.
[501043 Lecture 1: Intro to Java] 15
String and StringBuffer
There are many differences between String and
StringBuffer. A list of differences between String
and StringBuffer are given below:
[501043 Lecture 1: Intro to Java] 16
StringBuffer and String Builder
Java provides three classes to represent a sequence of
characters: String, StringBuffer, and StringBuilder. The
String class is an immutable class whereas StringBuffer
and StringBuilder classes are mutable. There are many
differences between StringBuffer and StringBuilder. The
StringBuilder class is introduced since JDK 1.5.
A list of differences between StringBuffer and
StringBuilder are given below:
[501043 Lecture 1: Intro to Java] 17
StringTokenizer
The [Link] class allows
you to break a string into tokens. It is simple
way to break string.
[501043 Lecture 1: Intro to Java] 18
StringTokenizer
[501043 Lecture 1: Intro to Java] 19
StringTokenizer
import [Link];
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("my name is kh
an"," ");
while ([Link]()) {
[Link]([Link]());
}
}
}
[501043 Lecture 1: Intro to Java] 20
StringTokenizer
import [Link].*;
public class Test {
public static void main(String[] args) {
StringTokenizer st = new StringTokenizer("my,name,is,khan");
// printing next token
[Link]("Next token is : " + [Link](","));
}
}
[501043 Lecture 1: Intro to Java] 21
Exercises
Give a Vietnamese fullname (ex “Nguyen Van Teo”).
Using StringBuffer or StringBuilder or StringTokenizer to
complete these exercises, Students need to write methods
as follows:
1. Count how many words in the name.
2. Return a first name
3. Return a last name
4. Return a middle name
5. Capitalize the first character in each word of the name
6. Formailize the name, including:
Delete spaces in front and behind of the name.
Leave one space between the words of the name.
[501043 Lecture 1: Intro to Java] 22
End of file