Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38960
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: assign lambda to functional interface |
| Date | 2019-05-11 09:38 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <qb6j84$acf$1@dont-email.me> (permalink) |
| References | <6594e2c1-ec44-4cbc-b6fb-2c8a852fa7dd@googlegroups.com> <qb689k$f0s$1@dont-email.me> <4a576120-782d-4519-bee1-14f1c85c9125@googlegroups.com> |
On 5/11/2019 8:57 AM, gk wrote:
> On Saturday, May 11, 2019 at 4:01:26 PM UTC+5:30, Eric Sosman wrote:
>> On 5/11/2019 12:58 AM, gk wrote:
>>> Can we assign a expression into an interface like this ?
>>> [... code snipped ...]
>>
>> Yes. Did something go wrong when you tried it?
>>
>> --
>> esosman@comcast-dot-net.invalid
>> Six hundred twenty-one days to go.
>
> This works . But that is a weird looking syntax. .... an expression going into interface which was not there in jdk 1.6 and below.
Is it "weird" that newer versions of Java add capabilities
not present before?
> also , can we do the same assignment to this to consumer also ?
>
> Consumer c = i -> (i < 18); // Consumer is functional interface.
... but its accept() method is void, so a boolean-valued
expression can't implement it. A different lambda could work.
> In fact can we assign this to any interface as well (non functional interface) ?
You can use a lambda expression as an instance of any functional
interface, because an F.I. has only one method and thus Java can
figure out which method the lambda implements.
You can use a lambda for a multi-method interface provided the
interface has exactly one abstract method and all the others have
`default' implementations. You can use a lambda for Consumer's
accept() method (because add() has a `default' version), but you
couldn't use a lambda for List.
--
esosman@comcast-dot-net.invalid
Six hundred twenty-one days to go.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
assign lambda to functional interface gk <srcjnu@gmail.com> - 2019-05-10 21:58 -0700
Re: assign lambda to functional interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-05-11 06:31 -0400
Re: assign lambda to functional interface gk <srcjnu@gmail.com> - 2019-05-11 05:57 -0700
Re: assign lambda to functional interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-05-11 09:38 -0400
Re: assign lambda to functional interface gk <srcjnu@gmail.com> - 2019-05-12 06:28 -0700
Re: assign lambda to functional interface Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-05-12 10:22 -0400
csiph-web