What's the difference between interface and @interface in java?

后端 未结 5 734
甜味超标
甜味超标 2020-12-02 03:44

I haven\'t touched Java since using JBuilder in the late 90\'s while at University, so I\'m a little out of touch - at any rate I\'ve been working on a small Java project th

相关标签:
5条回答
  • 2020-12-02 04:08

    interface: defines the contract for a class which implements it

    @interface: defines the contract for an annotation

    0 讨论(0)
  • 2020-12-02 04:11

    The interface keyword indicates that you are declaring a traditional interface class in Java.
    The @interface keyword is used to declare a new annotation type.

    See docs.oracle tutorial on annotations for a description of the syntax.
    See the JLS if you really want to get into the details of what @interface means.

    0 讨论(0)
  • 2020-12-02 04:15

    interface in the Java programming language is an abstract type that is used to specify a behavior that classes must implement. They are similar to protocols. Interfaces are declared using the interface keyword

    @interface is used to create your own (custom) Java annotations. Annotations are defined in their own file, just like a Java class or interface. Here is custom Java annotation example:

    @interface MyAnnotation {
    
        String   value();
    
        String   name();
        int      age();
        String[] newNames();
    
    }
    

    This example defines an annotation called MyAnnotation which has four elements. Notice the @interface keyword. This signals to the Java compiler that this is a Java annotation definition.

    Notice that each element is defined similarly to a method definition in an interface. It has a data type and a name. You can use all primitive data types as element data types. You can also use arrays as data type. You cannot use complex objects as data type.

    To use the above annotation, you could use code like this:

    @MyAnnotation(
        value="123",
        name="Jakob",
        age=37,
        newNames={"Jenkov", "Peterson"}
    )
    public class MyClass {
    
    
    }
    

    Reference - http://tutorials.jenkov.com/java/annotations.html

    0 讨论(0)
  • 2020-12-02 04:18

    The @ symbol denotes an annotation type definition.

    That means it is not really an interface, but rather a new annotation type -- to be used as a function modifier, such as @override.

    See this javadocs entry on the subject.

    0 讨论(0)
  • 2020-12-02 04:24

    interface:

    In general, an interface exposes a contract without exposing the underlying implementation details. In Object Oriented Programming, interfaces define abstract types that expose behavior, but contain no logic. Implementation is defined by the class or type that implements the interface.

    @interface : (Annotation type)

    Take the below example, which has a lot of comments:

    public class Generation3List extends Generation2List {
    
       // Author: John Doe
       // Date: 3/17/2002
       // Current revision: 6
       // Last modified: 4/12/2004
       // By: Jane Doe
       // Reviewers: Alice, Bill, Cindy
    
       // class code goes here
    
    }
    

    Instead of this, you can declare an annotation type

     @interface ClassPreamble {
       String author();
       String date();
       int currentRevision() default 1;
       String lastModified() default "N/A";
       String lastModifiedBy() default "N/A";
       // Note use of array
       String[] reviewers();
    }
    

    which can then annotate a class as follows:

    @ClassPreamble (
       author = "John Doe",
       date = "3/17/2002",
       currentRevision = 6,
       lastModified = "4/12/2004",
       lastModifiedBy = "Jane Doe",
       // Note array notation
       reviewers = {"Alice", "Bob", "Cindy"}
    )
    public class Generation3List extends Generation2List {
    
    // class code goes here
    
    }
    

    PS: Many annotations replace comments in code.

    Reference: http://docs.oracle.com/javase/tutorial/java/annotations/declaring.html

    0 讨论(0)
提交回复
热议问题