Deep learning the advanced use of strings in Java programming _java

Source: Internet
Author: User
Tags stringbuffer

Although Java is developed on the basis of C + +, but a lot of C + + defects have improved, one has to mention the string, we know, as learning, into MFC, when dealing with strings or characters, often need to use the _t () macro to convert characters or strings into Unicode type, Otherwise, there will be bugs in the process, and in Java, char or characters stored in the character class, not one byte, but 2 bytes, are Unicode, in order to support all the characters in the world.

The sequence of characters consists of strings, and there are two types of strings: one that you do not need to modify after you create it, called string constants, in Java, stored in string class;
One is the creation of a later need to modify it, called a string variable, in Java, with the StringBuffer class operations and management.

StringBuffer class

1. Create StringBuffer class objects

The StringBuffer class object represents a string variable (note "variable"), and each StringBuffer class object is a string variable that can be expanded and modified. The following are common StringBuffer class constructors:

(1) public stringbuffer ()

Creates an object with a new empty StringBuffer class whose capacity initial value is set to 16 characters (note is 16 characters)

(2) public stringbuffer (int length)

Creates an object of a new empty StringBuffer class whose capacity initial value is set to length characters

(3) Public stringbuffer (String str)

Creates an object of the new StringBuffer class whose contents are str and the capacity is set to str length plus 16 characters (note: plus 16 characters)


2, the common method of StringBuffer class object

(1) The extension of StringBuffer class objects

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

1) Public StringBuffer append

(Object obj)

The Append method is used to augment the characters contained in the StringBuffer object, which converts the specified parameter object to a string, attaches it after the original StringBuffer object, and returns 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 insertion position offset, parameter object type, Parameter object name

This method converts the specified Parameter object to a string, inserts it in the position specified in the original StringBuffer object, and returns the new StringBuffer object.

(2) The length and capacity of StringBuffer class objects

The length of a StringBuffer class object is the number of characters it contains, and capacity refers to the amount of 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 spaces allocated by the current StringBuffer class object.
(3) Modification of StringBuffer class objects

public void Setcharat (Intindex,charch)

This method replaces the character of 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 frequently used in a program, and the assignment and addition of strings are introduced into the Java compilation system.

(5) Other methods similar to the String class method
3, the use of StringTokenizer class decomposition string

The StringTokenizer class is in the Java.util package, and when you use the class, the program starts adding

Importjava.util.StringTokenizer or

Importjava.util.*

StringTokenizer class

For the StringTokenizer class, the main function is to split the string according to the given delimiter, and its function is similar to the split method of the String class

1, StringTokenizer class of constructors

(1) StringTokenizer (STRINGSTR)

Creates a StringTokenizer object for the given string str whose delimiter is set to "\t\n\r\f" by default, i.e.: Spaces, Horizontal tab tabs, wrapping, carriage returns, table characters

(2) StringTokenizer (String str,string Delim)

Creates a StringTokenizer object for the given string Str whose delimiter is the specified string Delim, and the default does not contain a delimiter


3) StringTokenizer (String str,string delim,boolean returndelims)

Creates a StringTokenizer object for the given string Str whose delimiter is the specified string Delim, and if Returndelims is true, each string in the StringTokenizer object that is created contains a delimiter. Otherwise, no delimiters are included

2, the common method of StringTokenizer class

Nintcounttokens ()
Returns the number of substrings in the StringTokenizer object that are split
Nbooleanhasmoreelements ()
The functionality of this method is the same as that of the Hasmoretokens () method
Nbooleanhasmoretokens ()
Detects if the StringTokenizer object contains a segmented substring, returns true if it returns false
Objectnextelement ()

The method has the same functionality as Nexttoken (), the main difference being that it returns not a string object but 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

N in some programming languages, such as C, the string is made up of character arrays, and the end of each string is marked with "yes", but not in Java.
N in Java, a string is usually present as an object of string class, such as: strings= "I like java!", where "Ilike java!" is an object.
So, the string and character array in Java is completely different, and the C language of the string is not the same!


N to facilitate the conversion of string and character arrays, many of these 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, it is uniformly saved to a constant pool for caching.
Compare the variables that refer to the strings that are cached in the constant pool, and use = = to get the correct result.

But at run time, the various operations on strings such as +, substring, and so on, will produce a new string object.
But the powerful compiler optimizes the concatenation of string constants, such as S3 = "hell" + "O", and S3 still
Points to a string in a constant pool. However, for the operation of variables, the virtual machine can not be required to perform such as S1 + s2
Determine if the result is already in the constant pool. Therefore, you should use equals rather than = = to determine whether two strings are equal.

public static void Main (string[] args) { 
 
 //String constants are the 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 code s1, s2, S3 byte code:

0:LDC #16; String Hello
2:astore_1
3:LDC #16; String Hello
5:astore_2
6:LDC #16; String Hello
8:astore_3

Test code S4, S5 byte code:
  
   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. " <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 from the character 0 to 2. The reason for this design
Perhaps it is so easy to compute the length of the substring, 3-0=3. At the same time, SUBSTRING has special optimized processing for special parameters:

Public String substring (int beginindex, int endindex) { 
 if (Beginindex < 0) { 
  throw new Stringindexoutofboundse Xception (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 a result, there is nothing magical behind string objects, and a bit of understanding of bytecode is a better way to understand it.
In fact, a constant pool also holds a lot of information about classes and their methods, such as package names, class names, method signatures, and so on, and you are interested in
In-depth study.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.