What is the Java Marker Interface? Introduction to Java Markup Interface
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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools