Over 10 Million Study Resources Now at Your Fingertips

Download as :
Rating : ⭐⭐⭐⭐⭐
Price : $10.99
Pages: 2

This code uses concept called deferred execution

Writing Simple Lambdas 211

Don’t worry that the syntax looks a little funky. You’ll get used to it and we’ll describe it in the next section. We’ll also explain the bits that look like magic. For now, just focus on how easy it is to read. We are telling Java that we only care about Animals that can hop.

The point here is that it is really easy to write code that uses lambdas once you get the basics in place. This code uses a concept called deferred execution. Deferred execution means that code is specifi ed now but will run later. In this case, later is when the print() method calls it.

Lambda Syntax

boolean test(Animal a);

Since that interface’s method takes an Animal, that means the lambda parameter has to be an Animal. And since that interface’s method returns a boolean, we know the lambda returns a boolean.

■ Specify a single parameter with the name a

■ The arrow operator to separate the parameter and body

a -> a.canHop()

■ The arrow operator to separate the parameter and body

■ A body that has one or more lines of code, including a semicolon and a return statement

optional parameter type

3: print(() -> true); // 0 parameters

4: print(a -> a.startsWith("test")); // 1 parameter

How It Works
Login account
Login Your Account
Add to cart
Add to Cart
Make payment
Document download
Download File
PageId: ELI28C83F7
Uploaded by :
Page 1 Preview
this code uses concept called deferred execution
Sell Your Old Documents & Earn Wallet Balance