Although JAVA is developed on the basis of C++, it has improved many shortcomings of C++. One of them that must be mentioned is strings. We know that with the deepening of learning, when entering MFC, when processing When strings or characters are used, it is often necessary to use the _T() macro to convert the characters or strings into UNICODE type. Otherwise, bugs will occur in the processing. In JAVA, the characters char or characters stored in the Character class are not One byte, but 2 bytes, using UNICODE, which is to support all characters in the world.
A sequence of characters constitutes a string. There are two types of strings: one does not need to be modified after creation, which is called a string constant. In JAVA, it is stored with the String class;
1 Those that need to be modified after creation are called string variables. In JAVA, they are operated and managed by the StringBuffer class.
StringBuffer class
1. Create StringBuffer class object
The StringBuffer class object represents a string variable (note that it is a "variable"). Each StringBuffer class object is String variables that can be expanded and modified. The following are commonly used StringBuffer class constructors:
(1) public StringBuffer()
Create a new empty StringBuffer class object, with its initial capacity value set to 16 characters (note is 16 characters)
(2) public StringBuffer (int length)
Create a new empty StringBuffer class object, and its initial capacity value is set to length characters
(3) public StringBuffer (String str)
Create a new StringBuffer class object, its content is the content of str, and the capacity is set to the length of str plus 16 characters (note: plus 16 characters)
2. Common methods of StringBuffer class objects
(1) Expansion of StringBuffer class objects
The StringBuffer class provides two sets of methods for extending StringBuffer The characters contained in the object are:
1) public StringBuffer append
(Object obj)
The append method is used to expand the characters contained in the StringBuffer object. This method After converting the specified parameter object into a string, append it to the original StringBuffer object and return the new StringBuffer object. Additional parameter objects can be of various data types, such as int, char, String, double, etc.
2) public StringBuffer insert (
int insertion position offset, parameter object type, parameter object name)
This method converts the specified parameter object into a string, Inserts it into the original StringBuffer object at the specified position and returns the new StringBuffer object.
(2) The length and capacity of StringBuffer class objects
The length of a StringBuffer class object refers to the number of characters it contains; the capacity refers to the number of allocated character spaces.
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 amount of character space allocated by the current StringBuffer class object.
(3) Modification of StringBuffer class object
public void setCharAt(intindex,charch)
This method replaces the character at the index position in the current StringBuffer object with the specified character ch.
(4) String assignment and addition
String is a data type that is frequently used in programs. String assignment and addition operations are introduced in the Java compilation system.
(5) Other methods are similar to those of the String class
3. Use the StringTokenizer class to decompose the string
The StringTokenizer class is located in the java.util package. When using this class, start the program at the beginning Add
importjava.util.StringTokenizer or
importjava.util.*
StringTokenizer class
For the StringTokenizer class, its main function is to convert strings into Split according to the given delimiter, its function is similar to the split method of the String class
1. 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" by default, that is: space, horizontal tab, newline, 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 the delimiter is not included 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 of the created StringTokenizer objects The string contains a delimiter, otherwise it does not contain a delimiter
2. Common methods of the StringTokenizer class
nIntcountTokens()
Returns the number of divided substrings in the StringTokenizer object
nBooleanhasMoreElements()
The function of this method is the same as the function of hasMoreTokens() method
nBooleanhasMoreTokens()
Detect whether the StringTokenizer object contains divided substrings, if so, return true, otherwise 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()
Returns 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 Set to delim
n. In fact, in some programming languages, such as C language, the strings are composed of character arrays, and the end of each string is marked with "\0", but in Java not like this.
nIn Java, strings usually exist as objects of the String class, such as: Strings="I like Java!", where "Ilike Java!" is an object.
So, strings and character arrays in Java are completely different, and they are also different from strings in C language!
nIn order to facilitate the conversion of strings and character arrays, many such constructors and methods are provided in the String class
nFor example, the constructor String(char[ ] value)
n method toCharArray()
method valueOf(char[] data)
Constant pool
For string constants that appear in the source program, when When the program is running, it will be uniformly saved in a constant pool for caching.
Compare variables that reference these strings cached in the constant pool, and use == to get the correct result.
But at runtime, various operations on strings, such as +, substring, etc., will generate new string objects.
However, powerful compilers will optimize the splicing of string constants. For example, when s3 = "hell" + "o", s3 will still
point to the string in the constant pool. But for variable operations, we cannot require the virtual machine to
determine whether the result is already in the constant pool when executing s1 + s2. 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); }
Bytecode of test codes s1, s2, s3:
0: ldc #16; //String hello
2: astore_1
3: ldc #16; //String hello
5: astore_2
6: ldc #16; //String hello
8: astore_3
The bytecode of the test code s4 and s5:
41: ldc #30; //String hell
43: astore 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. "7e51f00a783d7eb8f68358439dee7daf":(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 to get the string from characters 0 to 2. The reason for this design
may be that it is so 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); }
It can be seen from this that there is no magic behind the String object, and you can better understand it if you have some understanding of bytecode.
In fact, the constant pool also stores a lot of information about classes and their methods, such as package names, class names, method signatures, etc. If you are interested, you can study it in depth.
For more articles related to the advanced use of strings in Java programming, please pay attention to the PHP Chinese website!