Java object-oriented (eight): commonly used classes, enumerations, annotations

class

Commonly used classes:

System : The system class, which is the final class, inherits Object, and provides standard input, output, and error output streams.

Method :
System.currentTimeMillis(); Get current time: millisecond value
System.exit(0); Exit program
System.gc() normally ; Garbage collection
getProperty() method gets system properties.

Date : Date and time, divided into two, one is under the util package, the other is under the sql package (inheriting the Date class under the util package). The two can be converted to each other.

Date class under the Util package: java.util.Date

Two construction methods: empty construction and construction with millisecond parameter

Date()
allocates a Date object and initializes it so that it represents the time it was allocated, measured to the nearest millisecond.
Date(long date)
allocates a Date object and initializes it to the specified number of milliseconds after the standard base time called "Era", that is, 00:00:00 GMT on January 1, 1970.

SimpleDateFormat : Format the date, you can customize the format of the date, when
passing in the new construction method, pass in parameters to customize the format. Such as "yyyy-MM-dd HH:mm:ss"

Call the method
Date—>String
(format) format(), the passed parameter is a Date type, and the input date format is a custom format

String—>Date
(parse) parse(), the passed parameter is a String type, the format of this String must be the same as the custom format, otherwise a parsing exception will be thrown.

Code demo:
Insert picture description here
Insert picture description here
Insert picture description here

Calendar class: calendar class, is an abstract class. Cannot be created directly. Need to get the instance through the getInstance(); method. Use the default time zone and locale to get the calendar.

Method: The
add() method adds or subtracts a given calendar field to or from a specified amount of time according to the rules of the calendar.
The getTime() method converts Calendar to Date. The
setTime() method converts Date to Calendar. The
getTimeInMillis() method returns the time value of this calendar in milliseconds.

Introduction of date and time API in JDK8

LocalDate 、 LocalTime 、 LocalDateTime

Method:
now() get the instance, get the current time
of(int y,int M,int d,int H,int m,int s) get the instance, get the time according to the parameters
getXXX() can get the corresponding year, month, Day, hour, minute, second

Code demo:
Insert picture description here
Insert picture description here
Insert picture description here

Instant class: timestamp, instant. The
instance is also created by now(), the time obtained is the meridian time, and 8 hours need to be added to obtain the Beijing time
Insert picture description here

DateTimeFormatter : Date and time format
and SimpleDateFormat are similar to
Date—>String
(format) format(), the passed parameter is a Date type, and the input date format is a custom format

String---->Date
(analysis) parse(), the passed parameter is a String type, the format of this String must be the same as the custom format, otherwise a parse exception will be thrown.
Insert picture description here

Math class
Under the lang package, you can use it without importing the package, and the methods are all static methods, you can directly use the class name. The method name directly calls the method
• There are many methods, if you need to use it, look at the API! ! !

BigInteger class: large integer, java.math package provides processing large integer type, realizes the storage of large integers, four arithmetic operations, methods of judging prime numbers, exponentiation, modulus, inversion, and greatest common divisor.
There are methods of addition, subtraction, multiplication, and division

BigInteger a = new BigInteger("13343435454656576576767676767");
BigInteger b = new BigInteger("4959958585877477477474838");
//1.加
System.out.println(a.add(b));
//2.减
System.out.println(a.subtract(b));
//3.乘
System.out.println(a.multiply(b));
//4.除
System.out.println(a.divide(b));

BigDecimal : used to perform precise calculations on numbers with more than 16 significant digits

• The API class BigDecimal provided in the java.math package is used to perform precise operations on numbers with more than 16 significant digits. The double-precision floating-point variable double can handle 16 significant digits. In practical applications, it is necessary to perform operations and processing on larger or smaller numbers. Float and double can only be used for scientific calculations or engineering calculations. Java.math.BigDecimal should be used in commercial calculations.

 // 用字符串的形式初始化
        BigDecimal d1 = new BigDecimal("21233545464643232.983493849384");
        BigDecimal d2 = new BigDecimal("234545454545454545.232344454339933");
        //加法
        System.out.println("加法用string结果:" + d1.add(d2));
        //减法
        System.out.println("减法用string结果:" + d1.subtract(d2));
        //乘法
        System.out.println("乘法用string结果:" + d1.multiply(d2))
        //除法
        System.out.println("除法用string结果:" + 
        d1.divide(d2, 20, BigDecimal.ROUND_HALF_UP)); //四舍五入

Other : configure byte encoding: -encoding UTF-8 -charset UTF-8

Enum

Enumeration is a kind of specification, which regulates the form of parameters, so that you don’t need to consider the problem of type mismatch, and it explicitly replaces the fuzzy concept that int-type parameters may bring. Enumeration is like a class and another Array.

The parent class of enumeration is enum, the parent class of enum is Object, the construction method is private, and the instance cannot be created externally. The definition of the constant must be placed in front of the properties and methods (including the construction method). There can be methods and Can implement the interface

Use enumeration to obtain an instance: obtain an instance through the enum name. Constant; through the enum name. ValueOf(): The parameter is an enumeration constant.
Method: values() get all the constants in the enumeration and save them in the array. The values() method is a special method, not defined in the parent class (Enum). How did it come about? When the compiler compiles the enumeration type defined by enum, it automatically adds a static values() method.
Enumerations can be used in switch().

• For example:
– Season: spring, summer, autumn, winter
– Seven days a week: Monday...Sunday.
– Planets of the solar system: 8 planets.
– Gender: man, woman. etc.

• When we need to define a set of constants, we can consider defining an enumeration class. Enumeration types require object parameters to be finite, not infinite.
• enum is a keyword that defines an enumeration class.
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

annotation

Insert picture description here

What are meta annotations? It is a comment that modifies and explains the comment.

JDK provides four kinds of meta-annotations
@Retention: specify the life cycle of the modified annotation. Including SOURCE, CLASS (default behavior), RUNTIME.
According to the life cycle, it can be divided into 3 categories:

1. RetentionPolicy.SOURCE: The annotation is only kept in the source file. When the Java file is compiled into a class file, the annotation is abandoned;
2. RetentionPolicy.CLASS: The annotation is kept in the class file, but the jvm is abandoned when the class file is loaded. It is the default life cycle;
3. RetentionPolicy.RUNTIME: The annotation is not only saved in the class file, but still exists after the JVM loads the class file;

These three life cycles correspond to:
Java source file (.java file) —> .class file —> bytecode in memory.

@Target: Specify the modified annotations, which elements can be used.
Insert picture description here

@Documented: Specify the comment to be modified. When generating the API document, keep this comment in the document.

• See the @Deprecated annotation in the Date class, which is modified by this annotation, so you can see the reserved annotation in the API documentation.
Insert picture description here

@Inherited: Specifies that the modified annotation will have inheritance. If the parent class is modified, the child class automatically has this annotation.
Insert picture description here

The annotation modified by @Inherited can be inherited by subclasses, and this annotation is directly available.

Insert picture description here

Concluding remarks

I hope to communicate more and pay more attention
to continue learning "Java Object-Oriented (9): Collection"...
bye bye...

Guess you like

Origin blog.csdn.net/zhangzhanbin/article/details/112398484