Self-study JAVA-9: common methods of basic classes

1. System class
1. System.currentTimeMillis() Returns the current time in milliseconds from January 1, 1970 to the present.
2. System.gc(); is garbage collection. The user can speed up the garbage collection through the virtual machine through System.gc(), but cannot control the garbage collection. The finalize() method will be called to do some cleanup work before the object is collected.
3. System.getProperty() Get the value of the system parameter according to the name of the system parameter.
Note: @Deprecated marks this method as an obsolete method. There is a better way to replace it. This method can be used, but it is not recommended.

2. Date class
Calendar c = Calendar.getInstance(); Generate the calendar class object of the current time
c.add(Calendar.HOUR, 100); Calculate one hundred hours after the current time
int year = c.get(Calendar.YEAR); get year
int month = c.get(Calendar.MONTH); get month
int ri = c.get(Calendar.DATE); get day
Date di = new Date(); generate date object with current time
SimpleDateFormat sf = new SimpleDateFormat( "yyy year MM month dd day hh:mm:ss"); Build a date format object with the specified output format
String str = sf.format(di); Output the specified date object in the specified format

3. Packaging class The packaging class corresponding to the basic data types of automatic boxing and unboxing
:
basic data type packaging class
byte · Byte class
short Short class
int Integer class
long Long class
float Float class
double Double class
boolean Boolean class
char Character class
Note : The value contained in the wrapper class object is immutable.

Both have: The XXXValue() method displays the data encapsulated in the wrapper class. The
wrapper class name. The ParseXXX() method converts the string to the basic data type

Autoboxing: Allows the use of wrapper variables that receive data of primitive data types.
Integer x = 4;
During the implementation process, the data of the basic data type will be encapsulated into a wrapper class object and assigned to the wrapper class variable.
Equivalent to: Integer x = new Integer(4);

Auto-unboxing: Allows operations with wrapper class objects and data of primitive data types.
Integer y = new Ineger(5);
int z = y + 3;
During the implementation process, the XXXValue() method of the wrapper class object will be called to take out the basic data encapsulated in the wrapper class, and then perform operations.
Equivalent to: int z = y.intValue() + 3;

4. String class
String s = "abc";
String s1 = new String("abc"); The difference:
String s = "abc"; The variable s is the pointed string constant object, which is placed in the data segment.
String s1 = new String("abc"); The variable s1 points to the string object in the heap. The object needs to use the string constant in the data segment as a template, and then copy the content in the heap.
So it is said that new String("abc") produces two objects, one in the heap and one in the data segment.

String.charAt(int); Get the character with the specified subscript
String1.equals(String2) Compare two strings for equality, case-sensitive
String1.equalsIgnoreCase(String2) Compare two strings for equality, case-insensitive
int index = String1.indexOf(String2); Get the index of the first occurrence of String2 in String1, if the string does not exist, return -1
int index = String1.lastIndexOf(String2); Get the last occurrence of String2 in String1 The subscript that appears, if the string does not exist, return -1
String.length() Get the length of the string, which consists of characters
String1.replace(String2, String3) Replace the string Sring2 specified in String with a new one String String3
String.substring(int) starts from subscript int, intercepts the substring of Stirng string
String.substring(int1,int2) starts from subscript int1 and ends with subscript int2, intercepts String string Substring that contains the character with subscript int1, but does not contain the character with subscript int2
String.trim() Remove spaces at both ends of the string.

5. Regular expression
[]: match any character in []
{3,10}: The number of times the previous rule can be repeated, at least 3 times, at most 10 times
{3,}: The previous rule appears at least 3 times, No upper limit
|: or
\u4e00-u9fa5: Match Chinese characters
+: Equivalent to {1,} appearing at least once, no upper limit
*: Equivalent to {0,} can not appear, if it appears, it can appear multiple times, no Upper limit
?: Equivalent to {0,1} can not appear, it can only appear once
.: Any character
\d: Equivalent to [0-9], matching numbers
\D: Matching non-numbers
\w: Equivalent In [a-zA-Z0-9_], matches legal identifiers
\W: matches illegal identifiers
\s: matches spaces and carriage returns
\S: matches non-spaces and carriage returns

Strings have the characteristic of immutable length. When adding, deleting, or replacing strings, the content of the string will not be changed, but a new string object will be generated.

StringBuffer is the object of the buffer. When adding, deleting, and replacing StringBuffer data, no new object will be generated, and the content of StringBuffer will be directly changed, so StringBuffer can solve the shortage of String.

If you often operate on the content of the string, especially when the content needs to be modified, then use StringBuffer. If you need String at the end, then use StringBuffer's toString() method.

StringBuilder is thread-unsafe, StringBufer is out-of-the-box safe.
Performance: StringBuilder > StringBufer > String

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324615296&siteId=291194637