0% found this document useful (0 votes)
5 views8 pages

Venkatesh Maipathii

Uploaded by

MONICA NAHAK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Venkatesh Maipathii

Uploaded by

MONICA NAHAK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Dt : 25/9/2023

=>we can perform the following operations on StringBuffer Object:

(i)insert()

(ii)delete()

(iii)reverse()

ii
ath
Program : DemoBuffer5.java
package maccess;
public class DemoBuffer5

aip
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
hM
sb.append("java language program");
System.out.println("data in sb :
"+sb.toString());
System.out.println("*****insert()*****");
sb.insert(5, "HYD-NIT ");
tes

System.out.println("data in sb :
"+sb.toString());
System.out.println("****delete()*****");
a

sb.delete(5, 13);
System.out.println("data in sb :
nk

"+sb.toString());
sb.deleteCharAt(7);
Ve

System.out.println("data in sb :
"+sb.toString());
System.out.println("****reverse()*****");
sb.reverse();
System.out.println("data in sb :
"+sb.toString());
}
}
o/p:
data in sb : java language program

*****insert()*****

data in sb : java HYD-NIT language program

****delete()*****

data in sb : java language program

ii
data in sb : java laguage program

ath
****reverse()*****

data in sb : margorp egaugal avaj

aip
==================================================================

Assignment:
hM
wap to read a String and check the String is Palindrome String or not?

(Using reverse() method)

===================================================================
tes

Note:

=>StringBuffer class is known as "Synchronized Class".


a
nk

faq:
Ve

define Synchronized Class?

=>The class which is declared with "synchronized methods" is known as

synchronized class.

faq:
define synchronized methods?

=>The methods which are declared with "synchronized" keyword are known as

synchronized methods.

faq:

ii
wt is the behaviour of synchronized method?

ath
=>These synchronized methods will be under the synchronized-lock and the

methods are used by one user at-a-time.

Diagram:

aip
hM
a tes
nk
Ve

===================================================================
=

(c)StringBuilder:
=>StringBuilder class is from java.lang package and which also generate

Mutable Objects.

=>The following are some important constructors from the StringBuilder:

public java.lang.StringBuilder();

public java.lang.StringBuilder(int);

ii
public java.lang.StringBuilder(java.lang.String);

ath
public java.lang.StringBuilder(java.lang.CharSequence);

=>StringBuilder is having same behaviour like StringBuffer,but

aip
StringBuilder is NonSynchronized class.

=>NonSynchronized class means the class is holding NonSynchronized


hM
methods.

Note:
tes

=>In realtime,StringBuffer is used in Multi-User Applications and

StringBuilder is used in Single-User Applications.


a
nk

===================================================================
=====
Ve

faq:

define Utility Classes?

=>The Classes which perform operations on other Objects are known as

Utility Classes.

=>The following are two Utility classes related to String-Objects:


(a)StringTokenizer class

(b)StringJoiner class(Java8 - new Component)

(a)StringTokenizer class:

=>StringTokenizer class is from java.util package and which is used to

ii
break the given string into pieces(tokens) based on "delimiter"

ath
(delimiter - means break specification)

=>The following are some important methods of StringTokenizer:

public java.util.StringTokenizer

aip
hM
(java.lang.String, java.lang.String);

public boolean hasMoreTokens();

public java.lang.String nextToken();


tes

public java.lang.String nextToken(java.lang.String);

public boolean hasMoreElements();


a
nk

public java.lang.Object nextElement();

public int countTokens();


Ve

Program : DemoStringTokenizer1.java
package maccess;
import java.util.*;
public class DemoStringTokenizer1 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
try(s;){
System.out.println("Enter the String:");
String str = s.nextLine();
System.out.println("Enter the delimiter:");
String dl = s.nextLine();
StringTokenizer ob1 = new
StringTokenizer(str,dl);
StringTokenizer ob2 = new
StringTokenizer(str,dl);
System.out.println("Str : "+str.toString());

ii
System.out.println("Count of

ath
Tokens:"+ob1.countTokens());
System.out.println("****Tokens-
hasMoreElements()*****");
while(ob1.hasMoreElements())

aip
{
String ele = (String)ob1.nextElement();
System.out.println("Token :
"+ele.toString());
hM
}
System.out.println("Count of
Tokens:"+ob1.countTokens());
System.out.println("****Tokens-
hasMoreTokens()****");
tes

while(ob2.hasMoreTokens())
{
String ele = ob2.nextToken();
a

System.out.println("Token : "+ele.toString());
}
nk

System.out.println("Count of
Tokens:"+ob2.countTokens());
Ve

}//end of try with resource


}
}
o/p:

Enter the String:

java language program

Enter the delimiter:

ii
n

ath
Str : java language program

Count of Tokens:2

aip
****Tokens-hasMoreElements()*****

Token : java la
hM
Token : guage program

Count of Tokens:0

****Tokens-hasMoreTokens()****
tes

Token : java la

Token : guage program


a
nk

Count of Tokens:0

===============================================================
Ve
Ve
nk
ates
hM
aip
ath
ii

You might also like