Seven, reflection

The core of reflection is that the JVM dynamically loads classes or invokes methods/accesses properties at run time, and it doesn’t need to know who the running object is beforehand (at code writing or compile time).

Reflection can provide information about a class at run time, and the class can be loaded at run time, even if its.class does not exist at compile time.

Class provides support for reflection along with java.lang.reflect. The Java.lang. Reflect library consists of three classes:

Field: You can use the get() and set() methods to read and modify the fields associated with the Field object; Method: You can use the invoke() Method to call a Method associated with a Method object; Constructor: Use Constructor’s newInstance() to create new objects.

Advantages of reflection:

Extensibility: Applications can use user-defined classes from outside by creating instances of extensible objects with fully qualified names. Class browser and visual development environment: A class browser needs to be able to enumerate members of a class. Visual development environments, such as ides, can benefit from leveraging the type information available in reflection to help programmers write correct code. Debuggers and test tools: Debuggers need to be able to examine private members of a class. Reflection can be used by testing tools to automatically invoke discoverable API definitions defined in classes to ensure high code coverage in a set of tests.

Disadvantages of reflection:

Although reflexes are very powerful, they should not be abused. If a function can be done without reflection, it’s best not to. There are a few things to keep in mind when using reflection techniques.

Performance overhead: Reflection involves dynamic type resolution, so the JVM cannot optimize this code. Therefore, reflective operations are much less efficient than those that are not. We should avoid using reflection in code that is frequently executed or in programs that require high performance.

Safety restrictions: Using reflection techniques requires that the program be run in an environment with no safety restrictions. This is a problem if a program must run in an environment with security restrictions, such as an Applet.

Internal exposure: Because reflection allows code to perform operations that are not normally allowed (such as accessing private properties and methods), the use of reflection can cause unintended side effects that can cause code dysfunction and break portability. Reflecting code breaks abstractness, so when the platform changes, it is possible for the code to behave as well.

Eight, abnormal

Throwable can be used to represent any class that can be thrown as an Exception. There are two types of Throwable: Error and Exception. Error indicates an Error that cannot be handled by the JVM. Exceptions are classified into two types:

Exception: use try… catch… Statements are caught and processed, and can be recovered from exceptions; Unchecked Exception: a program runtime error, for example, dividing by 0 causes the Arithmetic Exception to crash and cannot be recovered.

Nine, generics

1. What are generics in Java? What are the benefits of using generics?

Generics prevent objects from being stored in collections and cast before they are used. It provides compile-time type safety, ensuring that you can only put objects of the correct type into collections, avoiding classcastExceptions at runtime.

2. How do Generics in Java work? What is type erasure?

Generics are implemented through type erasure, where the compiler erases all type-related information at compile time, so there is no type-related information at run time. For example, List is represented at run time by a single List. The goal is to ensure compatibility with the binary libraries developed prior to Java 5. You cannot access type parameters at run time because the compiler has converted generic types to primitive types.

3. What are qualified and unqualified wildcards in generics?

Qualified wildcards restrict types. There are two qualified wildcards. One is <? Extends T> This extends T> sets an upper bound on a type by ensuring that it must be a subclass of T. The other is <? Super T> it sets the lower bound of the type by ensuring that it must be a parent of T. A generic type must be initialized with a qualified type, otherwise a compilation error will result. On the other hand, <? > represents an unqualified wildcard because <? > can be substituted with any type.

4. List<? Extends T> and List <? What’s the difference between super T>?

Both List declarations are examples of qualified wildcards, List<? Extends T> accepts any List of type inherited from T, and List<? Super T> can accept any List of T’s superclasses. For example the List <? Extends Number> can take a List or List.

5. Can generics be used in Array?

No, because List provides compile-time type safety, whereas Array does not.

Ten, annotations,

Java annotations are meta-information attached to code that is parsed and used by tools during compilation, runtime, and configuration. Annotations do not and cannot affect the actual logic of the code, but only serve as an aid.

The difference between Java and C++

Java is a pure object-oriented language. All objects inherit from Java.lang. Object. C++ supports both object-oriented and procedural methods for compatibility with C.

Java implements cross-platform features through virtual machines, but C++ is platform-specific.

Java has no Pointers, and its references can be understood as safe Pointers, whereas C++ has the same Pointers as C.

Java supports automatic garbage collection, while C++ requires manual collection.

Java does not support multiple inheritance and can only implement multiple interfaces to achieve the same goal, while C++ does.

Operator overloading is not supported in Java, and while it is possible to add two strings, it is not supported by the language, as it is in C++.

Java’s goto is reserved but not available. C++ can use goto.

JRE or JDK

JRE: Java Runtime Environment (JRE) provides the required Environment for Java running. It is a JVM program that includes the standard implementation of the JVM and some basic Java class libraries.

JDK: Java Development Kit, Java Development Kit, provides Java Development and running environment. The JDK is the core of Java development, integrating JRE and other tools, such as the compiler javac that compiles Java source code. # References