Page 357 - Computer science 868 Class 12
P. 357
10 {
11 System.out.println(st.nextToken());
12 }
13 }
14 }
The output of the preceding program is as follows:
Number of Tokens 4
The different words in the sentence are:
I
am
studying
StringTokenizer
10.6 STRINGBUFFER CLASS IN JAVA
In Java, StringBuffer is a class that represents a alterable sequence of set of characters. This is used to create a string
that can be modified after it is created and works as an alternative to the String class. The String class is immutable and
cannot be changed once created.
The StringBuffer class in Java offers various methods for manipulating its contents. Here's a brief overview of the
methods you mentioned:
To create an object of the class, the methods used are
a. StringBuffer ob=new StringBuffer();
This is used to allocate space in memory to store a String.
b. StringBuffer ob= new StringBuffer("Computer Science");
This is used to allocate an object ob containing "Computer Science".
c. StringBuffer ob= new StringBuffer(100);
It allocates space in memory to conatin a String upto 100 characters.
StringBuffer Methods
The method used for StringBuffer class are as follows:
a. append(): The append() method is used to add characters, strings, or other objects to the end of the StringBuffer.
This method is overloaded to accept various data types, allowing convenient concatenation of different types of
data.
Syntax:
StringBuffer first_obj.append(last_obj);
Example:
StringBuffer first= new StringBuffer("Computer");
StringBuffer last= new StringBuffer(" Science");
System.out.println(first.append(last));
Output : Computer Science
b. insert(): The insert() method is used to insert characters, strings, or other objects at a specific position in the
StringBuffer. It takes the index at which the insertion should occur and the data to be inserted.
355
Strings 355

