Java commonly used system classes

The top-level root class of Java is Object

Object is the ancestor of all classes and defines the basic states and behaviors of all objects. When defining a class, even if the parent class is not specified, Java will automatically define it as a subclass of Object.

Java core package

There are 12 core Java packages, among which:
(1) java.lang package encapsulates the basic classes of all programming applications;
(2) java.util package provides utility classes and collection classes, such as system feature definition and usage, date Function classes, collections and other tools;
(3) The java.applet package provides Applet with all the classes needed for execution, mainly the communication classes for accessing the content of the Applet;
(4) The java.rmi package provides the classes required for remote method invocation.

System class in Java

The System class is a final class. Under the java.io package, all methods are called with class variables, and it is impossible to instantiate the System class. The System class is mainly used to provide access settings for standard input/output and system environment information. The properties of the System class are as follows:
(1) public static final InputStream in: standard input;
(2) public static final OutputStream out: standard output;
(3) public static final PrintStream err: standard error output.

String class in Java

In the String method,
(1) the toLowerCase() method is to convert to lowercase;
(2) The valueOf() method is to obtain the corresponding value;
(3) The charAt() method is to extract the character at the specified position in the string;
(4) append() is a method of StringBuffer (not in the String class).

StringBuffer class in Java

The methods for obtaining and setting the length and capacity of a StringBuffer object are length(), capacity(), setLength(), and the calling methods are as follows:
(1) sl.length(): returns the number of characters in sl; (both English characters and Chinese characters Is a string length)
(2) sl.capacity(): returns the capacity of sl, that is, the amount of memory space, usually greater than length;
(3) sl.setLength(int newLength): changes the number of characters in sl, if If newLength is greater than the original number, the newly added characters will be empty (" "); on the contrary, the last few characters in the string will be deleted.
example:

StringBuffer buf1=new StringBuffer(20);
System.out.println(buf1.length()+","+buf1.capacity());

result:

0,20

The distinction between String, StringBuffer and StringBuilder

1. Definition distinction :
String: String constant .
StringBuffer and StringBuilder are character buffer variables .

2. Thread safety distinction:
The methods and functions in StringBuffer and StringBuilder are completely equivalent, but most methods in StringBuffer are decorated with the synchronized keyword, so they are thread-safe , while StringBuilder does not have this modification and can be considered as Thread is not safe .

3. Running speed:
The "variable" characteristics of StringBuilder and StringBuffer are summarized as follows:
(1) The append, insert, and delete methods are basically calling the System.arraycopy() method to achieve the goal
(2) substring(int, int) The) method is to achieve the goal by renewing String(value, start, end-start). Therefore, there is basically no difference between StringBuilder and String when performing substring operations.
In general, the comparison of the three in terms of execution speed: StringBuilder> StringBuffer> String.
4. Usage scenarios:
(1) Scenarios using String class: The String class can be used in scenarios where the string does not change frequently, such as the declaration of constants and a small amount of variable operations.

(2) Scenarios using StringBuffer class: In frequent string operations (such as splicing, replacement, deletion, etc.) and running in a multi-threaded environment, you can consider using StringBuffer, such as XML parsing, HTTP parameter parsing and encapsulation.

(3) Scenarios using StringBuilder class: In frequent string operations (such as splicing, replacement, and deletion, etc.) and running in a single-threaded environment, you can consider using StringBuilder, such as assembling SQL statements, JSON packaging, etc. .
5. Distinguish:
(1) String objects of type String are immutable. Once the String object is created, the character series contained in this object cannot be changed until the object is destroyed;
(2) StringBuilder and StringBuffer The type of string is variable, the difference is that the StringBuffer type is thread-safe, while StringBuilder is not thread-safe;
(3) If it is a multi-threaded environment involving the insertion and deletion of shared variables, StringBuffer is the first choice . If it is a non-multithreaded operation and there are a large number of string splicing, insertion, and deletion operations, StringBuilder is the first choice. After all, the String class implements string splicing by creating temporary variables, which consumes memory and is not efficient. How to say StringBuilder implements the ultimate operation through JNI.

The string connection in the String class uses "+", while the connection in StringBuffer uses the append() method.
StringBuffer cannot be directly converted into String class object, you must call toString() method to turn a StringBuffer object into String class object.

The difference between string creation object and new creation object

Enclosed in double quotation marks are string constants (string), and enclosed in single quotation marks are character constants (char).

String s1="bc";
String s2="bc";
创建了一个对象

Java uses a string pool to manage strings, and the JVM creates a string buffer pool in memory when the Java program is running. For Java variable operations, memory is mainly divided into two parts: stack and heap. The stack is used to store basic types or built-in types, mainly char, byte, short, int, long, float, double, boolean, and object references. Data can be shared. The speed is second only to registers and faster than the heap. The heap is the memory space used to store objects, and the string buffer pool is the heap space. The heap can store implicit objects (such as string constants) or display objects (such as creating objects with new). In this example, when using String s1="bc" to create an object, the JVM first determines whether there is an object with a value of "bc" in the buffer pool, and if not, it constructs one so that s1 refers to the object. When s2 is created, the JVM finds an object with the same value in the buffer pool, then s2 refers to the object "bc" referenced by s1.

String s3="bc";
String s4=new String(s3);
创建了两个对象

In this example, the new operator is used to create the object, which will call the constructor of the String class to create a new object, which represents a sequence of strings that is the same as the parameter. In other words, the newly created string is a copy of the parameter string. Therefore, two objects are created: one is created in the string buffer pool in the form of string constants; the other is created in the heap by the new operator.

Matcher class in Java (Matcher)

(1) The boolean matcher.find() method is used to find the next pattern matching string;
(2) The int matcher.start() method is used to return a starting index integer value of the matching string;
(3) int matcher.end( The) method is used to return an integer value of the termination index of the matching string;
(4) The static boolean matches() method is used to compare the input string with the pattern string.

Guess you like

Origin blog.csdn.net/zhanlong11/article/details/114417367