Learning-Common classes

Source: Internet
Author: User

A. String class

String is the most used data type, in daily life, Internet use of various kinds of software issued by a variety of information is basically a string form; Java has designed the string class to be a very special class.

1. Features: The string class is the final type, cannot be inherited;

The string class overrides the Equals method to compare the contents of strings;

String can be used this way: string str = "Hello"; This is called a constant object, and the string constant is placed in the constant pool of the memory data area during the loading period and each string constant has only one;

String can also be new out of an object like a normal class: the string ste = new String ("Hello"), which produces two "Hello" strings, one in constant pool and one in heap space;

String class strings once generated content can not be changed, in the string concatenation operation or change the string object is actually produced a new string object, so in the case of string type strings modification will produce a large number of strings, inefficient;

The String class "" and null are two concepts, the former is an empty string, and the latter is null, so it is necessary to do a string non-null validation: if (str = = NULL) | | Str.equals ("") {} to use a short circuit or a judgment and must first determine the fee null, guaranteed that there will be no null pointer exception.

Common methods in 2.String:

Char Array related

ToCharArray method: Converts a string to a char array;

Length method: get string lengths;

Charat method: The position string is obtained according to the incoming subscript;

IndexOf method: Gets the position of the first occurrence of a character in the string, without which the character in the string returns-1;

LastIndexOf method: Gets the position of the last occurrence of a character in a string;

Substring method: Pass in two subscript, intercept the first coordinate to the second coordinate between the string return, front closed after opening;

Substirng Method (Overload): Pass in a subscript, intercept the subscript start to the last, return the string;

English case-related

toUpperCase method: All letters of the string are converted to uppercase;

toLowerCase method: Turn lowercase;

Equalsignorecase method: Ignore the case to determine whether the same;

CompareTo method: Compare Strings by English dictionary order;

Comparetotgnorecase method: Ignores the case by comparing strings in English dictionary order;

String content Correlation

Concat method: Connect the incoming string (equivalent to "+");

Contains method: Pass in a string to determine whether the string contains an incoming string;

StartsWith method: Pass in a string to determine whether the string starts with an incoming string;

EndsWith method: Pass in a string to determine whether the string ends with the passed-in string;

Replace method: Pass in two characters, replacing one character in the string (the first passed-in character) with the second passed-in character;

Replace method: Pass in two strings, replacing a string (the first passed-in string) with the second passed-in string;

Most commonly used

Trim method: Remove the front and back spaces of a character;

Split method: Passes in a character or string or regular expression, splits the string in the incoming data, returns string[] (if the split character appears in front or in the middle, even if it is joined together, the empty string is split, and a new element is not split at the end);

Matches method, the string to do regular expression validation, conforming to the canonical return true;

3. Three types of strings: String class, StringBuilder class; StringBuffer class; All three strings inherit from object, which is the final class;

The difference is the ability to directly use the + sign string concatenation only String, can directly string str = "Hello", only String, but once the object of the string class can not be changed, the other two classes of the string object is operable and can be changed, Therefore, the use of the other two classes when doing large string concatenation operations (large to affect the content stock);

StringBuilder class; StringBuffer class difference is: The StringBuffer class is thread-safe but inefficient; The StringBuilder class is thread insecure and efficient;

  

Two. Packing class

Eight basic data types each correspond to a wrapper class. The role of the wrapper class is:

1. Wrap the underlying data type as an object and provide a number of methods and constants to increase the richness of the underlying data type;

Such as:

int i = 100;

Integer in = new Integer (i);

can also: Integer in = i; (Auto-boxing, memory operation as above);

You can also convert the wrapper class to the underlying data type

such as: int i = IN; (Automatic unpacking)

2. Can be used as a tool class, providing tool methods that can directly convert the underlying data type to the reference data type;

such as: int i = 450;

String str = integer.tostring (i);

Or:

Stirng str = "270";

int i = Integer.parseint (str);

3. The wrapper class can also be converted to string;

Such as:

String str = "120";

Integer in = new Integer (str);//Call with parameter construct

Or:

Integer in = new Integer (320);

String str = in.tostring;

Three. Time, date class

The computer's save time is a number (in milliseconds 1 seconds = 1000 milliseconds), which records how many milliseconds the current distance time origin is. Now the time origin is (January 1, 1970 0 minutes 0 seconds 0 milliseconds);

The System.currnttimemillis method can get this number;

The most commonly used class that represents the current time: The Java.util.Date class, directly new a Date object calls the parameterless construct, can get the current time, with the Simpledataformat class (Simple time formatting), you can print the current time in a custom format.

Set a time to use the Calendar class, the way he produces objects is Factory mode, such as: Calendar cal = Calendar.getinstance ();

Note: The set month is 0月-November.

 

Learning-Common classes

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.