search
HomeJavajavaTutorialWhat 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.

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

What is the Java Marker Interface? Introduction to Java Markup Interface

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.

What is the Java Marker Interface? Introduction to Java Markup Interface

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.

What is the Java Marker Interface? Introduction to Java Markup Interface


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!

Statement
This article is reproduced at:segmentfault思否. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

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

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

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

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

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

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

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]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

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

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

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

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools