We need to know that when designing programs, we must often deal with issues related to character sequences. Therefore, the Java language specifically provides the String class for processing character sequences.
The String class is in the java.lang package . Since the classes in the java.lang package are introduced by default, the program can use the String class directly.
Note: Java declares the String class as a final class. Therefore, users cannot extend the String class. In other words, the String class cannot have subclasses.
So how to construct a string object? We can use the String class to create a string variable, and string variables are objects.
A string constant object is a character sequence enclosed in double quotes (note: under English input method), such as: "dotcpp", "123456", "C Language Network", etc.
We can declare string objects using the String class, for example:
Strings;
Because strings are objects, you must create string objects, for example:
s=newString(“wearestudents”);
Or we can create another string from an already created string, for example:
Stringtom=newString(s);
In addition, the String class has two more commonly used construction methods:
(1) String(char a[]) creates a string object using a character array a, for example:
chara[]={'d','o','t','c','p','p'};Strings=newString(a);
The above process is equivalent to:
Strings=newString(dotcpp);
(2) String(char a[], int startIndex, int count) extracts a part of the characters in the character array a to create a string object. The parameters startIndex and count specify the starting position and starting position of extracting characters in a respectively. The number of characters intercepted, for example:
chara[]={'d','o','t','c','p','p'};Strings=newString(a,3,3);
The above process is equivalent to:
Strings=newString(cpp);