Java notes: arrays, exceptions, generics

1、 Array

Array is also a reference type. Its parent class is object. It is declared with "data type []". For example, "int [] array" means that an array array with element type of int is declared.

Array initialization syntax:

When using arrays, you should pay attention to the following points:

Access and assignment of array: access and assignment can be done directly through subscript, such as "array1 [0] = 22;".

Advantages and disadvantages of arrays:

Array capacity expansion: the principle or method of array capacity expansion in Java is to copy small capacity arrays into large capacity arrays using the "system. Arraycopy" method, and then delete small capacity arrays. The capacity expansion efficiency of arrays in Java is low, so when using arrays, we should try to avoid array copying and capacity expansion.

Two dimensional array: a two-dimensional array, including a three digit array and other multi-dimensional arrays, is actually that the elements in the array are an array. The number of dimensions is actually determined by the "number of layers" of the elements in the array. A two-dimensional array is that the elements in the array are a one-dimensional array. Similarly, the elements in the three digit group are a two-dimensional array, and so on.

Arrays tool class: the two methods sort and binarysearch are most commonly used in this tool class. However, note that the premise of binary search method is that the array has been ordered.

2、 Abnormal

1. Understanding of exceptions

Exceptions are also classes. Each exception class can create an exception object.

Exception inheritance structure: you can see in the help document, Java lang.Object --> java. Lang. throwable. Under throwable, there are two subclasses, error and exception, which can be thrown. For these two subclass branches, Subclasses under the error branch (including the error itself) are called errors. Once an error occurs, it usually exits the program directly, and there is no way to deal with it in time. Subclasses under the exception branch (including the exception itself) is called an exception. Exceptions can be handled in advance at the code level. The subclass of exception can be divided into two branches. One branch is runtimeException and its subclasses, which are called runtime exceptions, and the other branch is the direct subclass of other exceptions except runtimeException, which is also called compile time exceptions.

Compile time exception: the reason why it is called compile time exception is that it can be found and reminded programmers to deal with this exception in advance at the compilation stage. For how to deal with this kind of exception in advance, we still have to practice more in actual use to have a deeper understanding.

Runtime exception: this kind of exception will not report an error when compiling, but it will make an error when running after compiling, so it is called runtime exception. For example, for the expression 10 / 0, the divisor of 0 must be wrong, but the compiler will not recognize and remind. After compiling, an error will be reported when running.

Exception handling: there are two ways to handle exceptions. One is to use the throws keyword and the throw keyword to throw exceptions to the upper level, Let the upper level handle the exception (the upper level must handle the exception at this time); the other is to use the "try... Catch" statement to catch the exception, but note that it is not necessary to handle the exception when it is caught, and it is allowed to "pass" it directly.

2. Throws throws an exception

Throw usage example:

For the use of throws, please note:

3. Try catch exception

Try usage example:

Note that the exception caught by catch can be either a specific exception or a parent type exception of a specific exception. At this time, it can be understood as polymorphic.

Common methods in exception objects:

4. Custom exception

Custom exception classes need to inherit exception or runtimeException, and need to define two constructors with theout and with the parameters.

When overriding or overriding a method in a custom exception, you need to pay attention to a syntax problem. The exception thrown by the overridden method cannot be larger or broader than that of the parent class, but smaller or more specific. For example, if the parent class exception method throws an IOException, the exception cannot be thrown when overriding this method in the abnormal subclass, However, you can throw IOException or FileNotFoundException.

3、 Generics

Generics are represented by angle brackets "< identifier 1, identifier 2,... >", and the identifier represents a type.

The function of generics is to define a template with it. The data type is not written when defining. When it is really used, you can pass in your own data type as needed.

Custom generics:

Application of generics in collection:

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
分享
二维码
< <上一篇
下一篇>>