System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə56/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   52   53   54   55   56   57   58   59   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

 Chapter 12 Self Test
1.
Enumeration constants are said to be self­typed. What does this mean?
2.
What class do all enumerations automatically inherit?
3.
Given the following enumeration, write a program that uses values( ) to show a list
of the constants and their ordinal values.


4.
The traffic light simulation developed in 
Try This 12­1
can be improved with a few
simple changes that take advantage of an enumeration’s class features. In the version
shown, the duration of each color was controlled by the TrafficLightSimulator class
by hard­coding these values into the run( ) method. Change this so that the duration of
each color is stored by the constants in the TrafficLightColor enumeration. To do
this, you will need to add a constructor, a private instance variable, and a method called
getDelay( ). After making these changes, what improvements do you see? On your
own, can you think of other improvements? (Hint: Try using ordinal values to switch
light colors rather than relying on a switch statement.)
5.
Define boxing and unboxing. How does autoboxing/unboxing affect these actions?
6.
Change the following fragment so that it uses autoboxing.
7.
In your own words, what does static import do?
8.
What does this statement do?
9.
Is static import designed for special­case situations, or is it good practice to bring all
static members of all classes into view?
10.
An annotation is syntactically based on a/an ________________ .
11.
What is a marker annotation?
12.
An annotation can be applied only to methods. True or False?


Chapter 13
Generics
Key Skills & Concepts

Understand the benefits of generics

Create a generic class

Apply bounded type parameters

Use wildcard arguments

Apply bounded wildcards
y
story
opics
utorials
ffers & Deals
ghlights
ettings
Support
Sign Out


S

Create a generic method

Create a generic constructor

Create a generic interface

Utilize raw types

Apply type inference with the diamond operator

Understand erasure

Avoid ambiguity errors

Know generics restrictions
ince its original 1.0 version, many new features have been added to Java. All have
enhanced and expanded the scope of the language, but one that has had an especially
profound and far­reaching impact is generics because its effects were felt throughout
the entire Java language. For example, generics added a completely new syntax element
and caused changes to many of the classes and methods in the core API. It is not an
overstatement to say that the inclusion of generics fundamentally reshaped the
character of Java.
The topic of generics is quite large, and some of it is sufficiently advanced to be beyond
the scope of this book. However, a basic understanding of generics is necessary for all
Java programmers. At first glance, the generics syntax may look a bit intimidating, but
don’t worry. Generics are surprisingly simple to use. By the time you finish this chapter,
you will have a grasp of the key concepts that underlie generics and sufficient
knowledge to use generics effectively in your own programs.
GENERICS FUNDAMENTALS
At its core, the term generics means parameterized types. Parameterized types are
important because they enable you to create classes, interfaces, and methods in which
the type of data upon which they operate is specified as a parameter. A class, interface,
or method that operates on a type parameter is called generic, as in generic class or

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   52   53   54   55   56   57   58   59   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə