A JAVA annotations

1. The concept

Annotations are a way and method provided by Java to associate information and metadata with elements in a metaprogram. An Annatation is an interface that allows a program to use reflection to get an Annotation object for a specified element in the program, and then use that Annotation object to get metadata information from the Annotation.

2. Four standard meta-annotations

The meta-annotation is responsible for annotating other annotations. Java5.0 defines four standard meta-annotation types that are used to provide annotations for other annotation types.

The scope of the object decorated by @target

@target specifies the range of objects that the Annotation decorates: Annotations can be used for packages, types (classes, interfaces, enumerations, Annotation types), type members (methods, constructors, member variables, enumerated values), method parameters, and local variables (such as loop variables, catch parameters). Target is used in the Annotation type declaration to clarify the target it modifies

@Retention Defines the length of time that is retained

Retention defines the length of time that the Annotation is retained: represents the level at which the Annotation information needs to be retained, and is used to describe the Annotation’s life cycle (i.e., the range in which the described Annotation is valid). The value (RetentionPoicy) is as follows:

„ SOURCE: valid in the SOURCE file (i.e. retained in the SOURCE file)

„ CLASS: valid in a CLASS file (i.e., CLASS reserved)

„ RUNTIME: valid at RUNTIME (i.e., reserved at RUNTIME)

@ Documented ᧿ – javadoc

Documented indicates that other types of annotations should be considered a public API for the annotated program member and therefore Documented by a tool such as Javadoc.

Inherited indicates that an annotated type is Inherited

An @Inherited meta-annotation is a markup annotation. @Inherited indicates that an annotated type is Inherited. If an annotation type with the @Inherited annotation is applied to a class, the annotation will be applied to a subclass of that class.

3. Annotation processor

Annotations are no more useful than annotations without methods and work to read them. An important part of working with annotations is to create an annotation handler that uses them. Java SE5 extends the reflection mechanism API to help programmers quickly construct custom annotation handlers. Let’s implement an annotation handler.

JAVA inner classes

Java classes can define not only variables and methods, but also classes, so that classes defined inside a class are called inner classes. According to the way of definition, inner class can be divided into static inner class, member inner class, local inner class and anonymous inner class.

1. Static inner classes

A static class defined inside a class is a static inner class.

1. A static inner class can access all static variables and methods of an external class, even if they are private.

2. Static inner class and general class, can define static variables, methods, constructors, etc.

3. Other classes using static inner classes need to use “outer classes”. Inner Inner =new out.inner (); inner.print();

4. The Java collection class HashMap contains a static inner class Entry. An Entry is an abstraction of the elements that a HashMap uses to maintain an Entry array, but the Entry is transparent to the user. Static inner classes can be used for things like this that are closely related to an external class and do not depend on an instance of an external class.

2. Member inner classes

A non-static class defined inside a class is a member inner class. Member inner classes cannot define static methods and variables (except for final modifications). This is because the in-member class is non-static, and the class initializes the static members first. If the in-member class is allowed to define static variables, the order in which the static variables are initialized is ambiguous.

3. Local inner classes (classes defined in methods)

Classes defined in methods are local classes. If a class is only used in a method, consider using a local class.

4. Anonymous inner class (to inherit a parent class or implement an interface, directly use

New to generate a reference to an object.

Anonymous inner classes we must either inherit from a parent class or implement an interface, or only inherit from a parent class or implement an interface. It also has no class keyword, because anonymous inner classes use new directly to generate a reference to an object.

3. JAVA generics

Generics provide a compile-time type-safety detection mechanism that allows programmers to detect illegal types at compile time. The nature of generics is parameterized typing, which means that the data type being operated on is specified as a parameter. For example, if we want to write a sorting method that can sort arrays of integers, arrays of strings, or any other type of array, we can use Java generics.

1. Generic methods (<E>)

You can write a generic method that accepts different types of arguments when called. The compiler handles each method call appropriately, depending on the type of argument passed to the generic method.

1. <? Extends T> means that the type represented by the wildcard is a subclass of type T.

2. <? Super T> indicates that the type represented by the wildcard is the parent of type T.

2. Generic class <T>

The declaration of generic classes is similar to that of non-generic classes, except that a type parameter declaration section is added after the class name. As with generic methods, the type parameter declaration section of a generic class contains one or more type parameters separated by commas. A generic parameter, also known as a type variable, is an identifier used to specify the name of a generic type. Because they take one or more arguments, these classes are called parameterized classes or parameterized types.

3. Type wildcard?

The type wildcard is usually used with? Instead of a specific type parameter. For example the List <? > is the logical parent of List<String>,List<Integer>, and all List< concrete type arguments >.

4. Type erasure

Generics in Java are basically implemented at the compiler level. The type information in the generics is not contained in the generated Java bytecode. Type parameters added when using generics are removed by the compiler at compile time. This process is called type erasure. Types such as List<Object> and List<String> defined in code become lists when compiled. All the JVM sees is the List, and the type information added by generics is invisible to the JVM. The basic process of type erasure is also relatively simple, starting with finding the concrete class to replace type parameters. This concrete class is usually Object. If an upper bound is specified for a type parameter, this upper bound is used. Replace all the type arguments in your code with concrete classes.

Iv. JAVA serialization (creating reusable JAVA objects)

Save (persist) objects and their state to memory or disk

The Java platform allows us to create reusable Java objects in memory, but in general these objects are only possible when the JVM is running, that is, they don’t have a lifetime longer than the JVM’s lifetime. In a real-world application, however, you might want to be able to save (persist) the specified object after the JVM stops running and re-read the saved object at a later date. Java object serialization helps us do this.

Serialized objects are held as byte arrays – static members are not saved

With Java object serialization, when an object is saved, its state is saved as a set of bytes that can be assembled into objects in the future. It is important to note that object serialization holds the object’s “state,” its member variables. Thus, object serialization does not care about static variables in a class.

Serialized user remote object transfer

In addition to using object serialization when persisting objects, object serialization is used when using RMI(remote method calls), or when passing objects over a network. The Java serialization API provides a standard mechanism for handling object serialization and is easy to use.

Serializable implements serialization

In Java, a class can be serialized as long as it implements the Java.io.Serializable interface. ObjectOutputStream and ObjectInputStream serialize and deserialize objects. ObjectOutputStream and ObjectInputStream serialize and deserialize objects.

WriteObject and readObject customize serialization policies

Adding writeObject and readObject methods to the class enables you to customize your serialization strategy.

Serialization ID

Whether the virtual machine allows deserialization depends not only on the same classpath and function code, but also on the same serialization ID of the two classes (private Static Final Long serialVersionUID).

Serialization does not hold static variables

Sequence class description of children and parents

To serialize a superclass object, you need to have the superclass implement the Serializable interface as well.

The Transient keyword prevents the variable from being serialized to a file

1. Add the Transient keyword before the variable declaration to prevent the variable from being serialized to the file. After deserialization, the Transient variable value is set to its initial value, such as 0 for int and NULL for object.

2. The server sends the serialized object data to the client. Some data in the object is sensitive, such as the password string, and the client wants to encrypt the password field during serialization. This ensures the data security of serialized objects to a certain extent.

JAVA replication

There are three ways to copy a reference from one object to another. The first method is direct assignment, the second is shallow copy, and the third is deep copy. So there you have it, all three of these concepts are actually for copying objects.

1. Direct assignment copy

Direct assignment. In Java, a1 = a2, we need to understand that this is actually copying A reference, meaning a1 and A2 refer to the same object. Therefore, when A1 changes, the member variables in A2 also change.

2. Shallow copy (copy references but not referenced objects)

Create a new object and copy the non-static fields of the current object into the new object, or if the fields are of value type. If the field is a reference type, the reference is copied but not the referenced object. Therefore, the original object and its copy refer to the same object.

3. Deep copy (copy objects and their applied objects)

The deep copy not only copies the object itself, but also all objects that the object contains references to.

4. Serialization (deep Clone 1 implementation)

In the Java language, it is often possible to make a deep copy of an object implement the Serializable interface, then write the object (which is really just a copy of the object) to a stream, read from the stream, and rebuild the object.