Câu hỏi phỏng vấn Java
Câu hỏi

What exactly is mark...

Câu trả lời

A marker interface in Java, also known as a tagging interface, is an interface that does not contain any methods, fields, or constants. Its primary purpose is to provide a way to associate metadata with a class that implements the interface, thereby conveying additional information about the class's behavior to the Java compiler and the Java Virtual Machine (JVM)[1][2][3][4][6][10].

Purpose and Functionality

Marker interfaces are used to indicate certain behaviors or capabilities of a class that implements them. For example, a class that implements the Serializable interface is flagged to the JVM as being capable of serialization, which is the process of converting an object's state to a byte stream so that the object can be easily saved to a file or transmitted over a network[1][2][6]. Other common marker interfaces include Cloneable, which indicates that the JVM can safely clone instances of the class, and Remote, used in Java's Remote Method Invocation (RMI) to indicate that an object can be called from a remote virtual machine[1][2][3][6].

Implementation and Usage

Implementing a marker interface is straightforward since the interface itself does not contain any methods to implement. The presence of the interface in a class's implementation declaration serves as a signal to the JVM and other components of the Java runtime environment. For instance, when a class is marked as Serializable, the JVM knows to treat instances of this class differently when serializing and deserializing objects[1][2][6].

Alternatives to Marker Interfaces

While marker interfaces have been a traditi...

senior

senior

Gợi ý câu hỏi phỏng vấn

middle

Which Swing methods are thread-safe?

junior

When does an Object becomes eligible for Garbage Collection in Java ?

senior

What is the difference between Serial and Throughput Garbage collector?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào