This document discusses StringBuffer, StringBuilder, and StringTokenizer in Java. StringBuffer and StringBuilder are used to create mutable strings, while String is immutable. The main differences between StringBuffer and StringBuilder are that StringBuffer is thread-safe and synchronized, while StringBuilder is non-synchronized. The StringTokenizer class allows a string to be broken into tokens based on a delimiter. Examples are provided for methods like append(), insert(), replace(), etc. on StringBuffer and exercises are provided to practice using these classes to manipulate Vietnamese names.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
38 views
3-StringBuffer StringBuilder
This document discusses StringBuffer, StringBuilder, and StringTokenizer in Java. StringBuffer and StringBuilder are used to create mutable strings, while String is immutable. The main differences between StringBuffer and StringBuilder are that StringBuffer is thread-safe and synchronized, while StringBuilder is non-synchronized. The StringTokenizer class allows a string to be broken into tokens based on a delimiter. Examples are provided for methods like append(), insert(), replace(), etc. on StringBuffer and exercises are provided to practice using these classes to manipulate Vietnamese names.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23
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 "); sb.append("Java");//now original string is changed System.out.println(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 "); sb.insert(1,"Java");//now original string is changed System.out.println(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"); sb.replace(1,3,"Java"); System.out.println(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"); sb.delete(1,3); System.out.println(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"); sb.reverse(); System.out.println(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 java.util.StringTokenizer 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 java.util.StringTokenizer; public class Simple{ public static void main(String args[]){ StringTokenizer st = new StringTokenizer("my name is kh an"," "); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } }
[501043 Lecture 1: Intro to Java] 20
StringTokenizer import java.util.*; public class Test { public static void main(String[] args) { StringTokenizer st = new StringTokenizer("my,name,is,khan");
// printing next token System.out.println("Next token is : " + st.nextToken(",")); } }
[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.