Detailed explanation of Java Fundamentals (generics, collection, IO, reflection)
I plan to look at some parts of the Java foundation again and consolidate them. I will share some notes of learning again below and in the future! Not all the knowledge points about the title, but some knowledge points that I feel vague.
1. For a generic class, if you do not specify its type, it defaults to object;
2. When inheriting generic classes and interfaces, you can specify the generic type or not;
3. Application of generic database:
Write a Dao class to add, delete, modify and query the data in the database, and its type declaration is < T >. Each table corresponds to a class, implements a class corresponding to each table, inherits the Dao class, indicates that the Dao generic is the class corresponding to the data table, and then implements a Dao operation class matching the table, so that it is not necessary to implement the basic method of adding, deleting, modifying and querying in the operation implementation class of each data table. For example (this is probably the idea in practical application, and the following examples are not complete):
4. Generic (static) cannot be used in static methods
Because static declared methods or classes and variables are initialized when the class is initialized, and generic types are initialized when running, there is a problem (the later calls occur first).
5. Wildcards
You can read a set declared as a wildcard, but you can't write elements into it (when reading, you can think of all elements as objects, but when writing, they are declared as?, not objects, that is to say, writing any type is wrong, but null can be stored), for example
6. Traversal of set map
7. Use reflection to obtain method name and attribute name, and use reflection to obtain constructor and other information
8. Use of comparator class (use comparator to realize user-defined sorting of collections)
Pay attention to distinguish between collections (collection processing class) and collections (collection base class)
9.IO
Reads the number of bytes of the target text file
Copy files (InputStream, OutputStream, reader and writer). The operation of text files is realized by using reader writer (character stream), which is efficient. However, media files cannot be operated; media files can be realized by using InputStream OutputStream, and text files can also be operated, but it is not as efficient as character stream.
10. Using buffer stream to copy files is more efficient
The above code summary is all the code and experience in your daily practice. The explanation of knowledge points is not comprehensive. I hope you can understand. Newcomers don't know how to write!
The above detailed explanation of Java Foundation (generics, collection, IO and reflection) is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.