Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Java Marker Interface?

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces what Java Marker Interface is, the content is very detailed, interested friends can use it for reference, I hope it can be helpful to you.

Let's take a look at what is the tagging interface? A tagged interface is sometimes called a tagged interface (Tag interface), which means that the interface does not contain any methods. It is easy to find examples of tagging interfaces in Java. For example, the Serializable interface in JDK is a tagged interface.

First of all, it is clear that Marker Interface (markup Interface) is by no means unique to the programming language Java, but a general design concept in computer science.

Let's look at the definition of the tagging 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 to translate the above passage by Google Translate, and it was very bad, so let me explain it.

Tag interface is a design idea in computer science. The programming language itself does not support maintaining metadata for classes. The tagging interface makes up for this lack of functionality-a class implements a tagged interface without any methods, which in fact becomes one of the metadata of the class in a sense. At run time, we can get this kind of metadata in the code through the reflection mechanism of the programming language.

Take the Serializable interface as an example. A class implements this interface, indicating that it can be serialized. Therefore, we actually mark the class with "serializable" metadata and "serializable" metadata through the Serializable interface. This is also the origin of the tag / label interface name.

The following code is extracted 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, strings, arrays, enumerated classes and normal classes in Java are done separately. If the current variable to be serialized is neither a string nor an array or enumerated class, then check whether the class implements the Serializable interface, as shown in line 1177 below. If the Serializable interface is not implemented, an exception NotSerializableException is thrown.

You might ask, isn't Annotation flying around 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, and so on for the purpose of maintaining metadata, which is both flexible and convenient. However, such a good thing can only be used after JDK1.5. Previously, the task of maintaining metadata in JDK1.5 fell on the tagging interface.

Let's look at another tagged interface, Cloneable. Line 51 of the following figure clearly indicates that the interface has been available since JDK1.0.

The comments on the Clone method in the JDK source code also make it clear that if a class does not implement the Cloneable interface, a CloneNotSupportedException exception will be thrown when the clone method is executed.

About what Java Marker Interface is to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report