-1

Came across this @Deprecated() today. Curious to know @Deprecated vs @Deprecated() but both are resolving to the same interface Deprecated by the compiler.

Are they both different or are they same? Is there some practice to use one over another?

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
  • If they both return to the same interface, why do you think they might be different? – M. Prokhorov Feb 21 '20 at 11:08
  • This is basic syntax of annotations (which this is) which can take parameters. The syntax for no parameters is no brackets or empty brackets, which are identical in effect. – Bohemian Feb 21 '20 at 11:21

2 Answers2

3

They both mean the same thing. @Deprecated is simply shorthand for @Deprecated(). See §9.7.2. Marker Annotations of the Java Language Specification:

A marker annotation is a shorthand designed for use with marker annotation types (§9.6.1).

MarkerAnnotation:
@ TypeName

It is shorthand for the normal annotation:

@TypeName()

It is legal to use marker annotations for annotation types with elements, so long as all the elements have default values (§9.6.2).

Example 9.7.2-1. Marker Annotations

Here is an example using the Preliminary marker annotation type from §9.6.1:

@Preliminary public class TimeTravel { ... }

As of Java 8 the @Deprecated annotation had no elements, so it could only ever be a marker annotation. Since Java 9, however, it now has two elements: since and forRemoval. But since those elements have default values the annotation can still be used as a marker annotation.

Slaw
  • 37,820
  • 8
  • 53
  • 80
0

It's not an interface, it is an annotation. @Deprecated and @Deprecated() are the same

Smile
  • 3,832
  • 3
  • 25
  • 39
User9123
  • 1,643
  • 1
  • 8
  • 21