Some trivial knowledge points of java learning

一, Note on using generics

1, when running a program, generic classes are shared by all implementations of such classes. See the case:

List<String> listString =new ArryList<>();
List<Integer> listInt =new ArryList<>();
System.out.printIn(listString.getClass()==listInt.getClass());//Print as true

Although the ArrayList<String> and ArrayList<Integer> types are treated as different types at compile time, actually In the edited bytecode class, the generics are erased, and the ArrayList<String> and ArrayList<Integer> types are treated as ArrayList types. So all runtime instances share a single runtime class.

2, ArrayList<String> and ArrayList<Integer> use the ArrayList<String> and ArrayList<Integer> parameters when defining the same name method, which is not an overload of the method. For the reason, see the above explanation. Look at the case:

public class test{

    public void Say(List<string> s1){


    public void Say(List<Integer> t1){



This method is illegal.

3, can not use the generic type to perform the cast of the type, this will have security risks, sometimes although the compiler can pass, but will throw an exception at runtime.

二, three implementation classes of the List interface

1, ArryList: more commonly used, the bottom layer is implemented by means of arrays, the query efficiency is high, the efficiency of addition and deletion is low, and the thread is not safe.

2, LinkedList: The underlying layer is realized by a doubly linked list. The efficiency of adding and deleting is high, the query efficiency is low, and the thread is not secure.

3, vector:Vector is a List that is implemented with arrays. The related methods are added with synchronization check, so "thread safe, low efficiency"

三, some methods of File

Note: The mkdir() and mkdirs() methods differ from

1 and mkdir: ensure that the parent directory exists, and if it does not exist, the creation fails.

2, mkdirs: Don't worry if the parent directory exists or not.