Although JAVA developed based on C++, it has improved many of the shortcomings of C++. One of the things that must be mentioned is strings. We know that with the deepening of learning, when entering MFC, when processing strings or characters, you often need to use the _T() macro to turn characters or strings into UNICODE type. Otherwise, a bug will occur during processing. In JAVA, the character char or characters stored in the Character class is not one byte, but 2 bytes. UNICODE is used, which is to support all characters in the world.
The sequence of characters forms a string, and there are two types of strings: one is a string constant that does not need to be modified after creation, which is called a string constant. In JAVA, it is stored using the String class;
One is a string variable that needs to be modified after creation, called a string variable. In JAVA, it is operated and managed by using the StringBuffer class.
StringBuffer class
1. Create a StringBuffer class object
The StringBuffer class object represents a string variable (note that it is a "variable"). Each StringBuffer class object is a string variable that can be expanded and modified. The following are the commonly used StringBuffer class constructors:
(1) public StringBuffer()
Create a new empty object of StringBuffer class, with the initial value of its capacity set to 16 characters (note that it is 16 characters)
(2) public StringBuffer (int length)
Create a new empty StringBuffer object, whose capacity initial value is set to length characters
(3) public StringBuffer (String str)
Create a new StringBuffer object, whose content is the content of str, and the capacity is set to the length of str and add 16 characters (note: add 16 characters)
2. Common methods of StringBuffer class object
(1) Extension of StringBuffer class object
The StringBuffer class provides two sets of methods to expand the characters contained in the StringBuffer object, namely:
1) public StringBuffer append
(Object obj)
The append method is used to expand the characters contained in the StringBuffer object. After converting the specified parameter object into a string, appends it after the original StringBuffer object, and returns the new StringBuffer object. The additional parameter objects can be of various data types, such as int, char, String, double, etc.
2) public StringBuffer insert (
int insert position offset, parameter object type, parameter object name)
This method converts the specified parameter object into a string, inserts it at the specified position in the original StringBuffer object, and returns the new StringBuffer object.
(2) The length and capacity of StringBuffer class object
The length of a StringBuffer class object refers to the number of characters it contains; capacity refers to the number of character space allocated.
1) public int length()
This method returns the number of characters contained in the current StringBuffer class object.
2) public int capacity()
This method returns the number of character space allocated by the current StringBuffer class object.
(3) Modification of StringBuffer class object
public void setCharAt(intindex, chearch)
This method replaces the character at the index position in the current StringBuffer object with the specified character ch.
(4) Assignment and addition of strings
Strings are data types that are often used in programs. The assignment and addition of strings are introduced in the Java compilation system.
(5) Other methods are similar to those of String class
3. Use the StringTokenizer class to decompose strings
The StringTokenizer class is located in the java.util package, and when using this class, it is added at the beginning of the program.
importjava.util.StringTokenizer or
importjava.util.*
StringTokenizer class
For the StringTokenizer class, its main function is to split the string according to the given split character, and its function is similar to the split method of the String class
1. The constructor of the StringTokenizer class
(1) StringTokenizer(Stringstr)
Create a StringTokenizer object for the given string str, whose delimiter is set to "/t/n/r/f", that is: space, horizontal tab, line break, carriage return, table character
(2) StringTokenizer(String str,String delim)
Create a StringTokenizer object for the given string str, whose delimiter is the specified string delim, and does not contain a delimiter by default.
3) StringTokenizer(String str,String delim,boolean returnDelims)
Create a StringTokenizer object for the given string str whose delimiter is the specified string delim. If returnDelims is true, each string in the created StringTokenizer object contains a delimiter, otherwise it does not contain a delimiter.
2. Common methods of StringTokenizer class
nIntcountTokens()
Returns the number of substrings in the StringTokenizer object after being split
nBooleanhasMoreElements()
The function of this method is the same as that of hasMoreTokens() method
nBooleanhasMoreTokens()
Detect whether the StringTokenizer object contains a divided substring. If so, it will return true, otherwise it will return false.
ObjectnextElement()
This method has the same function as nextToken(). The main difference is that it returns not a String object, but an Object object
StringnextToken()
Return the next split substring in the StringTokenizer object
StringnextToken(String delim)
Returns the next split substring in the StringTokenizer object, but the delimiter is reset to delim
nIn fact, in some programming languages, such as C, the string is composed of character arrays, and the end of each string is marked "/0", but this is not the case in Java.
nIn Java, strings usually exist as objects of String class, such as: Strings="I like Java!", where "I like Java!" is an object.
Therefore, strings and character arrays in Java are completely different, and they are also different from strings in C!
nIn order to facilitate the conversion of string and character arrays, many such constructors and methods are provided in the String class
n, such as constructor String(char[] value)
n method toCharArray()
Method valueOf(char[] data)
Constant pool
For string constants that appear in the source program, when the program runs, they will be saved to a constant pool for cache.
Comparing variables that reference these strings cached in constant pools will also get the correct result with ==.
However, at runtime, various operations on strings such as +, substring, etc. will produce new string objects.
But powerful compilers will optimize the stitching of string constants, such as when s3 = "hell" + "o", s3 will still point to the string in the constant pool. However, for variable operations, it is impossible to require the virtual machine to perform such as s1 + s2 and to determine whether the result is already in the constant pool. Therefore, use equals instead of == to determine whether two strings are equal.
public static void main(String[] args) { // String constants are put in constant pool. String s1 = "hello"; String s2 = "hello"; String s3 = "hell" + "o"; System.out.println(s1 == s2); System.out.println(s1 == s3); // Operation like +,substring on string create new one. String s4 = "hell"; String s5 = s4 + "o"; System.out.println(s1 == s5); System.out.println(s1.equals(s5)); // substring has special handle on substring(0) String s6 = s1.substring(0); System.out.println(s1 == s6); }Test the bytecodes of s1, s2, s3:
0: ldc #16; //String hello
2: store_1
3: ldc #16; //String hello
5: store_2
6: ldc #16; //String hello
8: store_3
Test the bytecodes of s4 and s5:
41: ldc #30; //String hell
43: store 4
45: new #32; //class java/lang/StringBuilder
48: dup
49: aload 4
51: invokestatic #34; //Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
54: invokespecial #40; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
57: ldc #43; //String o
59: invokevirtual #45; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
62: invokevirtual #49; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
Note that substring method, substring(0,3) is a string that gets from characters 0 to 2. The reason for this design may be that it is easy to calculate the length of the substring, 3-0=3. At the same time, substring has special optimization processing for special parameters:
public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex > count) { throw new StringIndexOutOfBoundsException(endIndex); } if (beginIndex > endIndex) { throw new StringIndexOutOfBoundsException(endIndex - beginIndex); } return ((beginIndex == 0) && (endIndex == count)) ? this : new String(offset + beginIndex, endIndex - beginIndex, value); } From this we can see that there is nothing magic behind the String object, and having some understanding of bytecode can better understand it.
In fact, a lot of information about classes and methods are stored in the constant pool, such as package names, class names, method signatures, etc. If you are interested, you can conduct in-depth research.