This article brings you what is the Java Marker Interface (Marker Interface)? The introduction of Java tag interface has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Let’s first look at what is a marker interface? Tag interface is sometimes called Tag interface, that is, the interface does not contain any methods. It is easy to find examples of marked interfaces in Java. For example, the Serializable interface in the JDK is a marked interface.
First of all, let’s make it clear that the Marker Interface is by no means unique to the Java programming language, but is a common method in computer science. design concept.
Let’s look at the definition of marker interface in Wikipedia.
“The tag/ marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata."
I tried Google Translate to translate the above paragraph, but the translation was very poor, so I will explain it.
Tagged interface is a design idea in computer science. The programming language itself does not support maintaining metadata for classes. The marker interface makes up for this lack of functionality - a class implements a marker interface without any methods. In fact, the marker interface becomes one of the metadata of this class in a sense. At runtime, through the reflection mechanism of the programming language, we can get this metadata in the code.
Take the Serializable interface as an example. A class implements this interface, indicating that it can be serialized. Therefore, we actually use the Serializable interface to mark the class with "serializable" metadata and label it as "serializable". This is where the tag/label interface gets its name.
The following code is taken from the JDK source code:
if (obj instanceof String) { writeString((String) obj, unshared); } else if (cl.isArray()) { writeArray(obj, desc, unshared); } else if (obj instanceof Enum) { writeEnum((Enum) obj, desc, unshared); } else if (obj instanceof Serializable) { writeOrdinaryObject(obj, desc, unshared); } else { if (extendedDebugInfo) { throw new NotSerializableException(cl.getName() + " " + debugInfoStack.toString()); } else { throw new NotSerializableException(cl.getName()); } }
Serialization in Java, strings, arrays, enumeration classes and ordinary classes are performed separately. If the variable currently to be serialized is neither a string nor an array or enumeration class, then it is checked whether the class implements the Serializable interface. Please note that line 1177 of the figure below performs this detection. If the Serializable interface is not implemented, an exception NotSerializableException will be thrown.
You may ask, aren’t the annotations flying all over the place in Spring the best way to maintain metadata? Indeed, Annotation can be declared in front of Java packages, classes, fields, methods, local variables, method parameters, etc. for the purpose of maintaining metadata, which is both flexible and convenient. However, such a good thing can only be used after JDK1.5. Before JDK1.5, the important task of maintaining metadata fell on the mark interface.
Let’s take a look at another marker interface, Cloneable. Line 51 of the figure below clearly indicates that this interface has been available since JDK1.0.
The comments on the Clone method in the JDK source code are also clearly stated. If a class does not implement the Cloneable interface, it will be executed when the clone method is executed. Throws CloneNotSupportedException.
The above is the detailed content of What is the Java Marker Interface? Introduction to Java Markup Interface. For more information, please follow other related articles on the PHP Chinese website!