What is JavaMarkerInterface

Let's first look at what a marker interface is. A tag interface is sometimes called a tag interface, which means that the interface does not contain any methods. It is easy to find examples of tagged interfaces in Java, for example the Serializable interface in the JDK is a tagged interface.

First, let's make it clear that the Marker Interface is in no way unique to Java as a programming language, but is a general design philosophy in computer science.

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 passage and it turned out poorly, so I'll explain.

Markup interfaces are 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 some marker interface without any methods, and in effect the marker interface becomes in a sense one of the metadata of the class. At runtime, through the reflection mechanism of the programming language, we can get this metadata in our code.

Take the Serializable interface as an example. The fact that a class implements this interface means that it can be serialized. So, we actually tag the class with "serializable" metadata through the Serializable interface. This is where the name of the tag/label interface comes from.

The following code was extracted from the JDK source code by me.

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 is done separately for strings, arrays, enumeration classes and normal classes. If the variable currently to be serialized is neither a string nor an array or enum class, then the class is tested to see if it implements the Serializable interface, and you will note that line 1177 of the figure below performs this test. If the Serializable interface is not implemented, an exception NotSerializableException is thrown.

One might ask, aren't the annotations (Annotation) flying around in Spring the best way to maintain metadata? Indeed, the ability to declare Annotation in front of Java packages, classes, fields, methods, local variables, method parameters, etc. for the purpose of maintaining metadata is both flexible and convenient. Yet something this good is only available after JDK 1.5. The burden of maintaining metadata before JDK 1.5 fell on the markup interface.

Everyone look at the other marker interface, Cloneable. Line 51 of the diagram below clearly marks that the interface has been available since JDK 1.0.

The comments of the Clone method in the JDK source code also clearly state that if a class does not implement the Cloneable interface, a CloneNotSupportedException will be thrown when the clone method is executed.

To get moreJerry of original technical articles, Please follow the public" Wang Zixi" Or scan the QR code below:

1、Gartners latest report Top 10 Strategic Technology Trends for 2016
2、MagnesiumKeeper Please speak Silan Technologys Chen Shikai Dedicated to solving the autonomous positioning and navigation capability for all kinds of robots
4、A brief analysis of WeakHashMap
5、LogEase A Case Study in Big Data Analytics for the Financial Payments Industry

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送