System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə63/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   59   60   61   62   63   64   65   66   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

Java: The Complete Reference, Eleventh Edition (Oracle Press/McGraw­Hill
Education, 2019).
 Chapter 13 Self Test
1.
Generics are important to Java because they enable the creation of code that is
A. Type­safe
B. Reusable
C. Reliable
D. All of the above


2.
Can a primitive type be used as a type argument?
3.
Show how to declare a class called FlightSched that takes two generic parameters.
4.
Beginning with your answer to question 3, change FlightSched’s second type
parameter so that it must extend Thread.
5.
Now, change FlightSched so that its second type parameter must be a subclass of
its first type parameter.
6.
As it relates to generics, what is the ? and what does it do?
7.
Can the wildcard argument be bounded?
8.
A generic method called MyGen( ) has one type parameter. Furthermore, MyGen(
) has one parameter whose type is that of the type parameter. It also returns an object
of that type parameter. Show how to declare MyGen( ).
9.
Given this generic interface
show the declaration of a class called MyClass that implements IGenIF.
10.
Given a generic class called Counter, show how to create an object of its raw
type.
11.
Do type parameters exist at run time?
12.
Convert your solution to question 10 of the Self Test for 
Chapter 9
so that it is
generic. In the process, create a stack interface called IGenStack that generically
defines the operations push( ) and pop( ).
13.
What is < >?
14.
How can the following be simplified?


Chapter 14
Lambda Expressions and Method References
Key Skills & Concepts

Know the general form of a lambda expression

Understand the definition of a functional interface

Use expression lambdas

Use block lambdas

Use generic functional interfaces
History
Topics
Tutorials
Offers & Deals
Highlights
Settings
Support
Sign Out


B

Understand variable capture in a lambda expression

Throw an exception from a lambda expression

Understand the method reference

Understand the constructor reference

Know about the predefined functional interfaces in java.util.function
eginning with JDK 8, a feature was added to Java that profoundly enhanced the
expressive power of the language. This feature is the lambda expression. Not only did
lambda expressions add new syntax elements to the language, they also streamlined the
way that certain common constructs are implemented. In much the same way that the
addition of generics reshaped Java years ago, lambda expressions continue to reshape
Java today. They truly are that important.
The addition of lambda expressions also provided the catalyst for other Java features.
You have already seen one of them—the default method—which was described in
Chapter 8
. It lets you define default behavior for an interface method. Another
example is the method reference, described later in this chapter, which lets you refer to
a method without executing it. Furthermore, the inclusion of lambda expressions
resulted in new capabilities being incorporated into the API library.
Beyond the benefits that lambda expressions bring to the language, there is another
reason why they constitute such an important part of Java. Over the past few years,
lambda expressions have become a major focus of computer language design. For
example, they have been added to languages such as C# and C++. Their inclusion in
Java helps it remain the vibrant, innovative language that programmers have come to
expect. This chapter presents an introduction to this important feature.
INTRODUCING LAMBDA EXPRESSIONS
Key to understanding the lambda expression are two constructs. The first is the lambda
expression, itself. The second is the functional interface. Let’s begin with a simple
definition of each.
lambda expression is, essentially, an anonymous (that is, unnamed) method.
However, this method is not executed on its own. Instead, it is used to implement a
method defined by a functional interface. Thus, a lambda expression results in a form


of anonymous class. Lambda expressions are also commonly referred to as closures.
functional interface is an interface that contains one and only one abstract method.
Normally, this method specifies the intended purpose of the interface. Thus, a
functional interface typically represents a single action. For example, the standard
interface Runnable is a functional interface because it defines only one method:
run(). Therefore, run() defines the action of Runnable. Furthermore, a functional
interface defines the target type of a lambda expression. Here is a key point: a lambda
expression can be used only in a context in which a target type is specified. One other
thing: a functional interface is sometimes referred to as a SAM type, where SAM stands
for Single Abstract Method.
Let’s now look more closely at both lambda expressions and functional interfaces.
NOTE
A functional interface may specify any public method defined by Object, such as
equals(), without affecting its “functional interface” status. The public Object
methods are considered implicit members of a functional interface because they are
automatically implemented by an instance of a functional interface.
Lambda Expression Fundamentals
The lambda expression relies on a syntax element and operator that differ from what
you have seen in the preceding chapters. The operator, sometimes referred to as the

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   59   60   61   62   63   64   65   66   ...   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ə