Ch13 Annotations Flashcards

1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
Q

Annotation elements are implicitely …

A

public and abstract

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
Q

What does the @Target ElementType value TYPE apply to?

A

Classes, interfaces, enums, annotations

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

What does the @Target ElementType value FIELD apply to?

A

Instance and static variables, enum values

21
Q

What does the @Target ElementType value METHOD apply to?

A

Method declarations

22
Q

What does the @Target ElementType value PARAMETER apply to?

A

Constructor, method and lambda parameters

23
Q

What does the @Target ElementType value CONSTRUCTOR apply to?

A

Constructor declarations

24
Q

What does the @Target ElementType value ANNOTATION_TYPE apply to?

A

Annotations

25
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.

26
Q

What does the @Target ElementType value LOCAL_VARIABLE apply to?

A

Local variables

27
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.

28
Q

What does the @Retention annotation do?

A

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

29
Q

What @Retention values exist?

A
  • SOURCE
  • CLASS
  • RUNTIME
30
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.

31
Q

What does the RetentionPolicy CLASS do?

A

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

32
Q

What does the RetentionPolicy RUNTIME do?

A

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

33
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.

34
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.

35
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.

36
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.

37
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.
38
Q

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

A

CLASS

39
Q

Can the @FunctionalInterface annotation be used for abstract classes?

A

No

40
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.

41
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
42
Q

What is the @SuppressWarnings annotation used for?

A

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

43
Q

What are two common @SuppressWarnings values?

A
  • “deprecation”
  • “unchecked”
44
Q

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

A

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

45
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< … >

46
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.

47
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).

48
Q

Which of the following annotations are retained for run time?

@SuppressWarnings
@Override
@SafeVarargs
@FunctionalInterface
@Deprecated

A

@SafeVarargs
It is defined with @Retention(RUNTIME)

@FunctionalInterface
It is defined with @Retention(RUNTIME)

@Deprecated
It is defined with @Retention(RUNTIME)