• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

(Sybex) Lambda expression

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dears,

I am not able to understand the explanation given in the book for the below code. Can someone please help me understand the same.

Options:
A. ok
B. too high
C. Compiler error on line x1.
D. Compiler error on line x2.
E. Compiler error on a different line.
F. A runtime exception is thrown.


answer:
C. The functional interface takes two int parameters. The code on line x1 attempts to use
them as if one is an Object, resulting in a compiler error making C the correct answer. It
also tries to return String even though the data type for the functional interface method is
boolean. It is tricky to use types in a lambda when they are implicitly specified. Remember
to check the interface for the real type.



Thanks in advance!!
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried actually compiling this code? What error did you get?
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It is tricky to use types in a lambda when they are implicitly specified. Remember
to check the interface for the real type.


If you try to compile the code and see the compiler error message you can start understanding the above statement. What type does the compiler error message mention?
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priyanka:

I too find these questions to make me nervous in a real test situation, because you are trying to go so fast.

There are many shortcuts we can take when using lambda expressions or method references, they allow us to type fewer characters, but require us to keep more "in our heads".

Let me ask you this, would you still have been confused if this line:


Was instead written out as this equivalent line?


If I didn't mess anything up, that is what the compiler will actually interpret the line as being.
When it is written out, it seems to simply be the garbage that it is.

The question is checking whether the shortcuts afforded to us in writing lambdas will obscure errors for us.
 
author & internet detective
Posts: 42056
926
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added "Sybex" to the subject. It's fine to post questions from the book, but we ask you to cite the source.
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic