Chapter 13: Annotations Flashcards

1
Q

What is metadata?

A

Metadata is data that provides information about other data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of annotations?

A

The purpose of an annotation is to assign metadata attributes to classes, methods, variables, and other Java types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Annotations function a lot like interfaces. Why use annotations over interfaces?

A

While interfaces can be applied to classes only, annotations can be applied to any declaration including classes, methods, expressions, and even other annotations.

Annotations also allow us to pass a set of values where they are applied.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the effect of annotations during program compilation?

A

There is none. Annotations can be removed and it will still compile and run, albeit with different behaviour.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the simplest form to define an annotation?

A

public @interface AnnotationName { }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an annotation definition that doesnt contain any elements also called?

A

A marker annotation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is the following code valid?

public @interface Exercise {
    int hoursPerDay();
}

@Exercise public class ZooEmployee { }

A

It is not valid and will not compile because the hoursPerDay field is required and must be assigned. (It is required because it contains no default value).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is the following code valid?

public @interface Exercise {
    int hoursPerDay();
}

@Exercise(hoursPerDay = 3) public class ZooEmployee { }

A

This code is valid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is the following code valid?

public @interface Exercise {
    int hoursPerDay();
    int startHour() default 6;
}

@Exercise(hoursPerDay = “3”) public class ZooEmployee { }

A

It won’t compile because the type given for hoursPerDay is not compatible with type int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Is the following code valid?

public @interface Exercise {
    int hoursPerDay();
    int startHour() default 6;
}

@Exercise(hoursPerDay = 3) public class ZooEmployee { }

A

This code is valid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Given the following code, how do we make hoursPerDay() default to value 24?

public @interface Exercise {
    int hoursPerDay();
}
A

int hoursPerDay() default 24;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can the default value of an annotation be null?

A

No, it must be a non-null constant expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the supported types of an annotation element?

A

A primitive type, a String, a Class, an enum, another annotation, or an array of any of these types (not multi-dimensional arrays).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Annotation elements are implicitely …

A

public and abstract

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can marker annotations contain constant variables?

A

Yes, they are not considered elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When can annotations be applied without paranthesis?

A

When the annotation is a marker annotation (contain no elements).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Where can annotations be applied?

A
  • Classes, interfaces, enums and modules
  • Variables (static, instance, local)
  • Methods and constructors
  • Method, constructor, and lambda parameters
  • Cast expressions
  • Other annotations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Which annotation can be used to specify which decleration type they can be applied to?

A

The @Target annotation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you mark an annotation element as required?

A

By declaring them without the default keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Given the following code, does Reptile compile?

public @interface Swimmer {
    int armLength = 10;
    String stroke();
    String name();
}

@Swimmer(stroke=”Butterfly”, name=”Kip”, armLength=1) class Reptile { }

A

It doesn’t because armLength is a constant, not an element, and can thus not be included in an annotation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What are the conditions for the use of an annotation with a value without specifying the element name?

example:
@Injured(“Broken Tail”) instead of @Injured(condition=”Broken Tail”)

A
  • The annotation declaration must contain an element names value(), which may be optional or required.
  • The annotation declaration must not contain any other elements that are required.
  • The annotation usage must not provide values for any other elements.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Does the following code compile?

public @interface Music {
String[] genres();
}

public class Reindeer {
    @Music(genres={}) String favorite;
}
A

Yes, this compiles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Does the following code compile?

public @interface Music {
String[] genres();
}

public class Reindeer {
    @Music(genres=) String favorite;
}
A

No, genres must be assigned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Does the following code compile?

public @interface Music {
String[] genres();
}

public class Reindeer {
    @Music(genres=null) String favorite;
}
A

No. It cannot be null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Does the following code compile?

public @interface Music {
String[] genres();
}

public class Reindeer {
    @Music(genres={"Rock", "Metal"}) String favorite;
}
A

Yes. It does.

26
Q

What does the @Target ElementType value TYPE apply to?

A

Classes, interfaces, enums, annotations

27
Q

What does the @Target ElementType value FIELD apply to?

A

Instance and static variables, enum values

28
Q

What does the @Target ElementType value METHOD apply to?

A

Method declarations

29
Q

What does the @Target ElementType value PARAMETER apply to?

A

Constructor, method and lambda parameters

30
Q

What does the @Target ElementType value CONSTRUCTOR apply to?

A

Constructor declarations

31
Q

What does the @Target ElementType value ANNOTATION_TYPE apply to?

A

Annotations

32
Q

What does the @Target ElementType value TYPE_USE apply to?

A

Able to be applied anywhere there is a Java type declared or used.

33
Q

What does the @Target ElementType value LOCAL_VARIABLE apply to?

A

Local variables

34
Q

is the java.lang.annotation package imported automatically by Java?

A

No. Even though java.lang is imported automatically, java.lang.annotation is not.

35
Q

What does the @Retention annotation do?

A

Specify when and if the annotation should be discarded by the copiler or at runtime.

36
Q

What @Retention values exist?

A
  • SOURCE
  • CLASS
  • RUNTIME
37
Q

What does the RetentionPolicy SOURCE do?

A

Specify that the annotation should be used only in the source file and discarded by the compiler.

38
Q

What does the RetentionPolicy CLASS do?

A

Specify that the annotation should be stored in the .class file but not available at runtime.

39
Q

What does the RetentionPolicy RUNTIME do?

A

Specify that the annotation should be stored in the .class file and available at runtime.

40
Q

What does the @Documented annotation do?

A

The documented annotation indicates that an annotation with this type should be documented by the javadoc tool. By default, annotations are not included in javadoc. But if @Documented is used, it then will be processed by javadoc-like tools and the annotation type information will also be included in the generated document.

41
Q

What does the @Inherited annotation do?

A

When an annotation with @Inherited is applied to a class, subclasses will inherit the annotation information found in the parent class.

42
Q

What does the @Repeatable annotation do?

A

The @Repeatable annotation is used when you want to specify an annotation more than once on a type.

43
Q

To declare a @Repeatable annotation, what must be defined as the value?

A

A containing annotation type which is a seperate annotation which defines a value() array element.

44
Q

What are the rules for the @Repeatable annotation?

A
  • The repeatable annotation must be declared with @Repeatable and contain a value that refers to the containing type annotation.
  • The containing type annotation must include an element named value(), which is a primitive array of the repeatable annotation type.
45
Q

What is the default Retention Policy value when @Retention is not used?

A

CLASS

46
Q

is the @Override annotation required when overriding a method?

A

No.

47
Q

is the @FunctionalInterface annotation optional?

A

Yes.

48
Q

Can the @FunctionalInterface annotation be used for abstract classes?

A

No

49
Q

What is the @Deprecated annotation used for?

A

To notify users of the code a newer version of a method is available and should migrate to it.

50
Q

What are the optional values for the @Deprecated annotation?

A
  • since() -> when the deprecation occured

- forRemoval() -> if the type is expected to be removed in the future

51
Q

What is the @SuppressWarnings annotation used for?

A

To tell the compiler that it should not bother the user with warning messages.

52
Q

What are two common @SuppressWarnings values?

A
  • “deprecation”

- “unchecked”

53
Q

What does the “deprecation” value for the @SuppressWarnings annotation do?

A

Ignore warnings related to types or methods marked with the @Deprecated annotation

54
Q

What does the “unchecked” value for the @SuppressWarnings annotation do?

A

Ignore warnings related to the use of raw types, such as List instead of List< … >

55
Q

What is the @SafeVarargs annotation used for?

A

To indicate that a method does not perform any potential unsafe operations on its varargs parameter.

56
Q

On what types can the @SafeVarargs annotation be applied?

A

Only to constructors or methods that cannot be overridden (methods marked private, static or final).

57
Q

Is @SafeVarargs a marker annotation?

A

Yes.

58
Q

Is @SuppressWarnings a marker annotation?

A

No.

59
Q

Is @Deprecated a marker annotation?

A

No.

60
Q

Is @FunctionalInterface a marker annotation?

A

Yes.

61
Q

Is @Override a marker annotation?

A

Yes.