Essentials of java knowledge (2)


I. set
1. Iterable v.s. Iterator
Both are interfaces, and the inherited interface in collection is Iterable.
Iteratable expresses that the collection has the ability of iterative access, while iterator represents the implementation, which can be from small to large or from large to small.

2. Comparable v.s. Comparator

3. set
    Collection needs multi-purpose

Two. Generic
1. ListAs a concrete type, unreal logical type
List l1 = new ArrayList< String >();
List l2 = new ArrayList< Integer >();
System.out.println(l1.getClass() == l2.getClass() );

Therefore, static members of a class cannot use type parameters.
    public class R{
        Static t info; // error
        Static void test (t MSG); // error

2. ?, ? extends type, ? super type      
    1) Wildcard and inheritance relationship
    There is no inheritance relationship between list and list.
        Void test (List L) passing in list will compile and report an error

    Wildcards and their upper and lower limits can be understood as supplements to inheritance relationships.
    Void test (list > L) function can receive list, list, etc
    Void test (list extensions numbers > L) function can receive list, list, etc.

    2) Understand the following code, where rect is a shape subclass
        void addRect(List< ?  extends Shape> shapes){
            shapes.add(0, new Rect());

Three, abnormal
The purpose of exception design is to separate business logic from exception handling logic.
When an exception occurs in the running process of a java program, the system automatically generates an exception object, which is submitted to the Java runtime;
When the Java runtime receives the exception object, it looks for the appropriate catch block. If not, the Java program exits.

1. try...catch...finally
    The finally code block will be executed unless try or catch calls System.exit.
    Return, throw in try or catch will be executed after the finally block

2. Abnormal system
        -> Error  
        -> Exception
            -> RuntimeException
            ->Others...: checked exception must show handling