Learn more about the advanced use of strings in Java programming

  • 2020-04-01 04:38:47
  • OfStack

JAVA was developed based on c + +, but many defects of c + + improved, one has to ask is a string, as we know, with the deepening of the study, enters the MFC, when dealing with string or Character, often need to pass _T () macro into UNICODE Character or string type, otherwise, will appear in dealing with the BUG, and in JAVA, Character char or stored in the characters in a Character class, not a byte, but 2 bytes, using UNICODE, this is in order to support the all of the characters in the world.

              The sequence of characters constitutes a String, there are two types of strings: one is created after the need for no modification, called String constant, in JAVA, with the String class storage;
        One, which needs to be modified after it is created, is called string variables, which in JAVA are manipulated and managed using the StringBuffer class.

  StringBuffer class

1. Create a StringBuffer class object

        A StringBuffer object represents a string variable (note the "variable "), and each StringBuffer object is a string variable that can be extended and modified. The following are the commonly used StringBuffer class constructors:

          (1) public StringBuffer ()

            Creates a new empty StringBuffer class object with an initial capacity of 16 characters (note 16 characters)

    (2) public StringBuffer (int length)

            Creates a new empty StringBuffer class object with its initial capacity set to length characters

      (3) public StringBuffer (String STR)

            Creates a new StringBuffer class object with the contents of STR and the capacity set to the length of STR plus 16 characters (note: plus 16 characters)


2. Common methods of StringBuffer class objects

        (1) StringBuffer class object expansion

                The StringBuffer class provides two sets of methods to extend the characters contained in the StringBuffer object, respectively:

              1) public StringBuffer append

                                                      (Object  Obj)

                  The append method extends the characters contained in the StringBuffer object by converting the specified parameter object to a string, attaching it to the original StringBuffer object, and returning the new StringBuffer object. Additional parameter objects can be of various data types, such as int, char, String, double, and so on.

2) public StringBuffer insert (

              Int insert position offset, parameter object type, parameter object name)

      This method converts the specified parameter object to a string, inserts it at the specified location in the original StringBuffer object, and returns the new StringBuffer object.

      (2) the length and capacity of the StringBuffer object

      The length of a StringBuffer object is the number of characters it contains. 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 number of character Spaces 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) assignment and addition of strings

          String is a data type that is often used in programs, and the assignment and addition of strings were introduced into the Java compilation system.

    (5) other methods are similar to methods of the String class
3. Decompose the string with the StringTokenizer class

The StringTokenizer class is in the java.util package and is added at the beginning of the program when the class is used

Importjava. Util. StringTokenizer or

Importjava. Util. *

The StringTokenizer class

For the StringTokenizer class, the main function is to split the String according to the given separator, which is similar to the String class's split method

1. Constructor of the StringTokenizer class

(1) the StringTokenizer (Stringstr)

                Creates a StringTokenizer object for the given string STR with its separator set to "\t\n\r\f" by default, i.e., space, horizontal TAB, newline, carriage return, and table character

(2) StringTokenizer(String STR,String delim)

                          Creates a StringTokenizer object for the given string STR with the specified string delim delimiter, which by default contains no delimiter

 


3) StringTokenizer(String STR,String delim, Boolean returnDelims)

                        Creates a StringTokenizer object with the specified string delim delimiter for the given string STR, and if returnDelims is true, each string in the StringTokenizer object created contains a delimiter, otherwise no delimiter

2. Common methods of the StringTokenizer class

NIntcountTokens ()
Returns the number of segmented substrings in the StringTokenizer object
NBooleanhasMoreElements ()
This method does the same thing as the hasMoreTokens() method
NBooleanhasMoreTokens ()
Detects whether the StringTokenizer object contains a segmented substring, and returns true if so, or false if not
ObjectnextElement ()

This method does the same thing as nextToken(), except that instead of returning a String Object, it returns an Object

StringnextToken ()

Returns the next segmented substring in the StringTokenizer object

StringnextToken (String delim)

Returns the next segmented substring in the StringTokenizer object, but the delimiter is reset to delim

In some programming languages, such as C, strings are arrays of characters that end with an "\0" flag, but this is not the case in Java.
In Java, Strings usually exist as objects of the String class, such as Strings= "I like Java!" , where "Ilike Java!" It's just an object.
So, strings in Java are not the same as arrays of characters, and strings in C are not the same!

 


N to facilitate the conversion of strings and character arrays, a number of such constructors and methods are provided in the String class
N like the constructor String(char[] value)
N method toCharArray ()
Methods the valueOf (char [] data)


Constant pool

String constants that appear in the source program are stored in a constant pool for caching when the program runs.
Comparing variables that reference these strings cached in the constant pool, using == also gives the correct result.

At run time, however, various operations on strings such as +, substring, and so on generate new string objects.
But powerful compilers optimize concatenation of string constants such as s3 = "hell" + "o"
Points to a string in a constant pool. But for the operation of a variable, you can never ask a virtual machine to perform an operation such as s1 + s2
Determine if the result is already in the constant pool. Therefore, use equals instead of == to determine if 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 and s3:

    0:     Ldc        # 16. / / the String hello
    2:     astore_1
    3:     Ldc        # 16. / / the String hello
    5:     astore_2
    6:     Ldc        # 16. / / the String hello
    8:     astore_3

Bytecode of test codes s4 and s5:
   
    41:   Ldc        # 30; / / the String to hell
    43:   Astore  4
    45:   New        # 32; / / class Java/lang/StringBuilder
    48:   dup
    49:   Aload    4
    51:   Invokestatic      # 34; / / Method Java/lang/String. The 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 the substring method, substring(0,3), is the string that gets the characters 0 to 2. The reason for the design
Maybe it's easy to figure out the length of the substring. 3 minus 0 is 3. Meanwhile, substring has special optimization treatment 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); 
} 

As you can see, there's nothing magical about String objects, and you can understand them better with some understanding of bytecode.
In fact, the constant pool also holds a lot of information about the class and its methods, such as package name, class name, method signature, and so on, if you are interested
Dig deeper.


Related articles: