• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

What is deferred execution and how do I know when it's being used?

 
Sam Peterson
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On pages 210 - 211 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, there is a code sample that demonstrates lambda expressions. It basically prints out the names of animals based on a boolean of whether they can swim or hop:



On page 211, the book says, "This code uses a concept called deferred execution. Deferred execution means that code is specified now but will run later. In this case, later is when the print() method calls it."

I don't understand. The lambda print method on lines 16 and 17 seem to be listed in the main method just like any method calls would. So what difference is 'deferred execution' supposed to make here?
 
Stephan van Hulst
Saloon Keeper
Posts: 14490
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What it means is that the calls to canHop() and canSwim() are deferred. Normally when an expression contains a nested method call, that method call is evaluated immediately. In this case however, canHop() and canSwim() are called when print calls test().

Here's another example:

Calling person.printName() could result in an output like this:

So even though getFirstName() appears before getLastName() on line 12, getLastName() is executed first. We say execution of getFirstName() has been deferred.
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe, Stephan, you would like to explain why deferred execution is desirable. The example we discussed a few weeks ago (here) might show its benefits better.
 
Sam Peterson
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this:

Line 6 gives the error, "Syntax error, insert ";" to complete Statement", even though the book claims that it's valid syntax. What's wrong?
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 6 isn't a statement; it might be if it had a second ; at the end of it in which case it might become a kind of expression statement. Please check carefully what the book says, to verify where the actual error is.
 
You have to be odd to be #1 - Seuss. An odd little ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic