Exception, collection, generic

Posted by coreysnyder04 on Mon, 31 Jan 2022 22:44:01 +0100

I Exception definition
In Java, the errors that may occur when a program runs are divided into two categories: exception and Error. Exception refers to the Error that can continue to run after being corrected by the programmer, which is called exception
In the process of method execution, if an exception occurs, the exception method or JVM will generate a corresponding exception object. This object contains the exception type and program running state, which is called throw.

Throwable includes error and exception. Exception also includes runtime exception (RuntimeException, also known as non check exception) and non runtime exception (also known as check exception)

 

Null pointer exception (NullPointerException)

String str=null;
System.out.println(str.length());

subscript out of bounds
 

int[] ary={1,2,3}
for{int i=0;i<=3;i++){
System.out.println(ary[i]);
}

Type conversion exception

class Animal{
}
class Dog extends Animal{
}
class Cat extends Animal{
}
public class Test{
public static void main(String[] args){
Animal a1=new Dog();
Animal a2=new Cat();
Dog d1=(Dog)a1;
Dog d2=(Dog)a2;

Arithmetic anomaly

int one=12;
int two=0;
System.out.println(one/two);

ClassCastException: type cast exception

 

package com.darkmi.basic;

public class Test {

public static void main(String[ args) {testParse("aa");}}

 

Assemble
A collection class is used to store certain objects.
Collection is a basic collection interface, which can accommodate a set of collection elements.
Map does not inherit the Collection interface, and it has a parallel relationship with Collection. Map provides a key to value mapping. A map cannot contain the same key, and each key can only map one value.
Collection has two important sub interfaces, List and Set.
 

A Collection allows the same elements while others do not. Some can be sorted while others can't. The Java SDK does not provide classes directly inherited from Collection. The classes provided by the Java SDK are all "sub interfaces" inherited from Collection, such as List and Set

List features: elements are placed in order, and elements can be repeated. Set features: elements are not placed in order, elements cannot be repeated, and repeated elements will be overwritten,
 

ArrayList
 

transient Object[] elementData

LinkedList
 

private static class Node<E> {
    E item;
    Node<E> next;
    Node<E> prev;
    
}

Vector
 

protected Object[] elementData;


 

 

Generics

Simple generic class
 

public class Generics<T> { 
    private T o;   
    public Generics(T o){  
        this.o=o;  
    }  
    public T getObject() {  
        return o;  
    }  
    public void setObject(T o) {  
        this.o = o;  
    }  
    public String getType() {  
        return o.getClass().getName();  
    }  
}


public class # fanxingleidemo < T > this is a generic type, which can pass arbitrary parameters

Generic class

private T obj;
public T getObj() {
return obj;
}
public void setObj(T obj) {
this.obj = obj;
}
}

Generic method:
 

public class FanXingMethod {
public <T> void show(T t)
{
System.out.println(t);
}
}

Generic interface:
 

public interface Inter<T> {
public abstract void show(T t);
}

wildcard
 

void printCollection(Collection<Object> c){
for (Object e : c) {
System.out.println(e);
}
}

 

Considerations for using generics

Type parameters of generic types can only be class types (including custom classes), not simple types. We can use versions of various class types of this generic class, but generics do not support basic data types.
A generic type can have more than one type parameter.
The instanceof operation cannot be used on an exact generic type. If the following operation is illegal, an error will occur during compilation.