4

Should Java annotations be considered a language or library feature. I don't mean the very concept of annotations which is obviously a language feature, but specific annotations such as @Override and @Deprecated?

It seems clearer that annotations supported only by 3rd-party libraries (e.g. Hibernate annotations) are clearly not part of the Java language, but for the built-in annotations I'm not so sure.

If this is considered too discursive, feel free to move to programmers.stackexchange.com

3 Answers 3

4

Here's how I think about it:

boolean isLanguageFeature(foo)
{
    return JLS.contains(foo)
}

The JLS describes Java the programming language. As you clearly understand, the concept of annotations is a language feature. On the other hand, there are tons of standard Java library features which are not described by the JLS, such as the Collections APIs.

From JLS §9.6.1, Predefined Annotation Types:

Several annotation types are predefined in the libraries of the Java platform. Some of these predefined annotation types have special semantics. These semantics are specified in this section. This section does not provide a complete specification for the predefined annotations contained here in; that is the role of the appropriate API specifications. Only those semantics that require special behavior on the part of the Java compiler or virtual machine are specified here.

Thinking along those lines, then, I'd say that Java annotations are a library feature.

Sign up to request clarification or add additional context in comments.

Comments

2

Annotations like @Deprecated and @Override are library features that happen to be used by the language tools to enforce and/or describe code behavior. Definitely library features.

5 Comments

I agree with you, but you haven't actually provided any supporting argument.
They're in the standard Java library. By definition, they're library features. I don't feel a strong need to support that, I guess.
Supporting argument: @Override is likely the most language -ish of the standard annotations. Yet, if you wrote an alternative Java compiler that ignores this annotation completely, the resulting code would behave exactly the same as the one javac produces. (Assuming it was correct in the first place.) The only way an annotation can change the behaviour of code is if a library (like Hibernate) processes them with a bytecode enhancer.
The fact that @Override (library feature) is not required to actually override a function (language feature) makes it even more an (optional) library feature. Omitting @Override does not convey different behavior in runtime, only at compile time (extra checks) and in the IDE (warnings). See stackoverflow.com/questions/4822954/…
@xdevs23 ... Yes, that's what we said.
-2

Annotations are used (in Java) for adding meta data to your code. Annotations are the way to write readable and maintainable code. Therefore to write good code use annotations weather they are Java annotations or any other third party.

for e.g. like to make an EJB stateless now you just need to add @Stateless annotation.

2 Comments

I believe his question has more to do with the nature of Java annotations not what they are or when they should be used.
Agreed with @tke. The OP clearly understands how and why to use annotations. This answer misses the question and entirely fails to conclude either way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.