Page 324 - CITS - Computer Software Application -TT
P. 324
COMPUTER SOFTWARE APPLICATION - CITS
A String is essentially a sequence of characters. In Java, String objects are immutable, signifying that they are
constant and cannot be altered after their creation
CharSequence Interface: The CharSequence interface in Java serves as a representation for sequences of
characters. Below are some of the classes that implement the CharSequence interface
1 String
2 StringBuffer
3 StringBuilder
1 StringBuffer: StringBuffer is a companion class to String in Java, and it offers extensive functionality for
manipulating character sequences. While strings in Java are immutable and represent fixed-length character
sequences, StringBuffer represents character sequences that can dynamically grow and be modified.
StringBuffer demoString = new StringBuffer(“ctiworld”);
2 StringBuilder: In Java, the StringBuilder class represents a mutable sequence of characters. Unlike the String
class, which creates immutable character sequences, StringBuilder provides an alternative that allows you to
create and modify mutable character sequences
StringBuilder demoString = new StringBuilder();
demoString.append(“CTI”);
3 StringTokenizer: The StringTokenizer class in Java is employed for splitting a string into tokens or smaller
components.
Example
Indeed, a StringTokenizer object in Java keeps track of an internal current position within the string that it’s
tokenizing. Certain operations cause this position to move forward as characters are processed. To obtain a
token, the StringTokenizer object extracts a substring from the original string used to create it.
StringJoiner is a class within the java.util package in Java, designed for constructing a sequence of characters
(strings) separated by a specified delimiter. It also offers the option to begin with a provided prefix and end with a
supplied suffix. While similar functionality can be achieved using the StringBuilder class by manually appending
a delimiter after each string, StringJoiner simplifies this process, reducing the amount of code you need to write.
Syntax:
public StringJoiner(CharSequence delimiter)
As mentioned earlier, you can create a string in Java using a string literal. A string literal is a sequence of characters
enclosed within double quotation marks
String myString = “Hello, World!”;
311
CITS : IT&ITES - Computer Software Application - Lesson 78 - 84
78