Java notes: common classes

For the use of built-in classes and methods in Java, we can usually read the corresponding API documents, but we still need to be proficient in using some commonly used classes and methods.

@H_ 419_ 6 @ I. system

@H_ 419_ 6@System.gc (): start the garbage collector manually. The garbage collector usually starts automatically. Sometimes Java may think that the current situation does not need to start GC, but if you want to start it again, you can call this method to start GC manually.

@H_ 419_ 6@System.exit (0): terminate and exit the JVM. The exit method can pass in a program end status code, usually 0.

@H_ 419_ 6@System.currentTimeMillis (): gets the total number of milliseconds from 00:00:00 000 on January 1, 1970 to the current time.

@H_ 419_ 6@System.arraycopy (object SRC, int srcpos, object DeST, int destpos, int length): copy several elements of continuous length from the source array SRC to the target array dest. SRC is the source array, srcpos is the starting subscript of the source array, DeST is the target array, destpos is the starting subscript of the target array, and length is the number or length of copied elements.

@H_ 419_ 6 @ II. Object

@H_ 419_ 6@protected Object clone(): returns the clone of the object. Note that the modifier of this method is protected. It can only be used under subclasses or the same package.

@H_ 419_ 6@int Hashcode (): returns the hash value of the object.

@H_ 419_ 6@boolean Equals (object obj): judge whether two objects are equal. By default, "= =" is used for judgment. Note that the judgment of "= =" actually judges the value of the variable. For the reference type, the comparison is the memory address, but this is usually not the result we want, because for the comparison of two objects, we mostly think of the objects of the same class. If their properties and other object contents are the same, the two objects can also be considered to be the same, Instead of judging their memory addresses, you need to rewrite this method at this time. It can also be seen that "= =" is used to judge equality in ordinary times if it is a basic data type. If it is a reference data type, such as string, its own equals method needs to be used.

@H_ 419_ 6@String Tostring(): converts an object into a string. The default value is "class name @ memory address of the object". When printing an object, this method of the object will also be called automatically. However, because the default return value of this method is often not what we want, we usually override it when we need to use it instead of using its default value.

@H_ 419_ 6 @ III. string

@H_ 419_ 6@ string constant pool in the method area: Although the string is also an instance object of reference type, the string literal is stored in the string constant pool in the method area. Whether it is defined with double quotation marks or string, a string object will be created in the memory of the method area first. If the same string is used later, Instead of creating another string object, the existing string object will be used directly@ H_ 419_ 6@String Type reference: it should be noted that a string object defined directly with double quotation marks will directly create a string object in the method area. However, because the new keyword will first create the corresponding object in the heap, for creating a string object with the new keyword, such as "string s = new string (" hello ");", Although the created object is in the heap, the value of S (memory address) actually refers to the string object "hello" in the string constant pool in the method area. @ h)u 419)u 6 @ string garbage collection: in addition, the garbage collector will not recycle the string constant object in the string constant pool.

@H_ 419_ 6@ common construction methods:

@H_ 419_ 6 @ common methods:

@H_ 419_ 6 @ IV. StringBuffer / StringBuilder

If a large number of string splicing operations are required, it is recommended to use Java's own StringBuffer or StringBuilder instead of the plus sign +.

@H_ 419_ 6@StringBuffer Example:

@H_ 419_ 6 @ on the optimization of StringBuffer: one of the optimization points is to estimate the required string length during initialization and give a larger length (the default array has only 16 bytes) to reduce the number of subsequent array expansion. The key point is that the length cannot be too large or too small. Too large also wastes space, and too small will increase the number of expansion.

@H_ 419_ 6 @ compare string: the bottom layer of string is actually a byte [] of final type, and the bottom layer of StringBuffer is also a byte [], but the difference is that there is no final modifier, because once the length of the array is determined, it cannot be changed, This represents a variable of type string modified by final (Reference) the memory address pointed to cannot be changed, that is, the string cannot be changed. However, if there is no final modifier, the array can be expanded when it is full, and the variable of StringBuffer can point to the memory address of the new array after expansion. When it comes to the final of string, it should be noted that for example, "string s =" hello " In general, final modifies the string object "hello", not the variable s (Reference), which can be re assigned.

@H_ 419_ 6 @ packaging class of basic data type in V and 8

The reason why the wrapper class exists is to deal with a situation. The parameters of some methods need to be object type, but they also need to be passed into the basic data type in Java. Because the basic data type is incompatible, it must not be passed in. At this time, it is necessary to use the wrapped class of the basic data type, which belongs to the reference data type, And their final parent class is the object class. At this time, you can pass in these methods for use.

@H_ 419_ 6@ wrapper classes corresponding to basic data types: except that the wrapper classes corresponding to int and char types are slightly different, other types of wrapper classes are capitalized by their own words.

@H_ 419_ 6@ packing: converting the basic data type to the reference data type (corresponding packing class) is called packing.

@H_ 419_ 6@ unpacking: converting a package object to a basic data type (not necessarily the original basic data type) is called unpacking.

@H_ 419_ 6 @ examples of manual packing and unpacking:

@H_ 419_ 6 @ automatic packing and automatic unpacking: the above example is not commonly used.

@H_ 419_ 6 @ integer constant pool: in Java, in order to improve the execution efficiency of the program, all wrapper objects between [- 128127] are created in advance and placed in the integer constant pool in the method area. In the future, as long as these objects are used, the new object will not be used, just like the string constant pool.

@H_ 419_ 6@Integer Common methods: because the usage of wrapper classes are mostly the same, and different parts can be understood by referring to the help document, only the common methods of integer class are shown here.

@H_ 419_ 6 @ VI. date / simpledateformat

The "Java. Util. Date" class is used for date processing in Java, and the "Java. Text. Simpledateformat" class is used for date formatting.

@H_ 419_ 6 @ usage example:

@H_ 419_ 6@ date and time format characters are as follows:

@H_ 419_ 6 @ VII. Number formatting

"Java. Text. Decimalformat" should be used for number formatting. If dealing with big data or financial data, "Java. Math. BigDecimal" should be used because of its high precision. Please refer to the API documentation for specific use@ H_ 419_ 6 @ partial formatting characters:

@H_ 419_ 6 @ VIII. Random number

To generate a random number, you should use "Java. Util. Random". Please refer to the API documentation for detailed usage.

@H_ 419_ 6 @ IX. enumeration

Enumeration type, keyword enum, is also a reference data type. After compiling an enumeration, a ". Class" bytecode file will also be generated.

Each value in the enumeration can be regarded as a constant, and each enumeration value only needs to define the corresponding name without assigning a value to it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>