Internal organs Heart--Java Tag interface

Source: Internet
Author: User

Write in front of the words: reading broken million, coded as God
--------------------------------------------------------------------
This blog is mainly about "Java Tag interface" Knowledge, the main content includes:

1. Overview

2. Serializable

3. cloneable

4. randomaccess

--------------------------------------------------------------------
1. Overview

Java commonly used three tags interface is:randomaccess, cloneable, Serializable, when viewing the JDK source code, we will often find that these interfaces exist, they do not contain any method, but there is a wide range of existence, This interface we call labeled Interface (Mark Interface), which we do not implement any method, their role is when a class implementation of this interface, we think that the class has a function of the interface tag.

2, Serializable

Java.io.Serializable This interface is used to mark whether a class supports serialization, and so-called serialization is the conversion of various information of an object into a form that can be stored or transmitted. If a class does not implement the interface and is taken to serialize, the virtual machine throws an exception that does not support serialization.

3, Cloneable

The Cloneable interface is often used in deep copy, as follows:

1  Public Interface cloneable{2 }

Its role is to mark whether the object has the ability to clone, it may feel a bit strange here, we all know that the Java.lang.Object class itself has a method:

1 protected native Object Clone ()  throws clonenotsupportedexception

As a matter of principle, each class should be able to run the Clone method just right, why do you need such a markup interface? The advantage of this is that the object is labeled as an interface that has some kind of capability. Think, if not through the form of the marking interface, we in peacetime development, how to achieve it? In general, by adding variables or setting enumerations to achieve the effect of control, this may solve the problem, but often can not from the object-oriented angle to solve the problem gracefully. Think about what the interface is all about? Interfaces are used to mark what functions and features a class has, while a markup interface is a more advanced abstraction in an object-oriented perspective: Even if you have this method, because you do not have a markup interface for this function, in the process of invoking the Clone method, If the object does not implement the Cloneable interface, then the virtual machine throws a Clonenotsupportedexception exception.

4, Randomaccess

1  Public Interface randomaccess () {2 }

The purpose of this interface is to determine whether the set can be accessed quickly, that is, the index can be quickly moved to the corresponding element. We are using a collection class, the elements in the collection can be quickly accessed by Indexing Index Index, then at the definition of the class, there will generally be an implementation label for the Randomaccess interface

For example,:java.util.arraylist<e> has this interface,java.util.linkedlist<e> there is no

 1  public  class  arraylist<e> extends  abstractlist<e> 2  implements  list<e> 3  { 4  //  ...  5 } 
1  Public class Linkedlist<e>2     extends abstractsequentiallist<e>3     implements list<e>, deque<e>, cloneable, java.io.Serializable4{5     ... 6 }

--------------------------------------------------------------------

--------------------------------------------------------------------

Internal organs Heart--Java Markup Interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.