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

Java 8 Lambda Expressions

 
Ranch Hand
Posts: 391
1
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading about Java 8 Lambda Expressions and I understood what it is, its syntax and where to use it.

Now I understand what is a functional interface and how we can refactor code using anonymous inner class to code using Lambda Expressions.

Consider this code



In case of Anonymous classes we create an instance of anonymous class implementing Runnable interface


But by seeing this code it look like we are assigning a function to Runnable, is it that under the hood an anonymous class implementing Runnable interface is created. Or something else.

Its hard to digest that we are assigning a lambda expression to some interface type. One is a interface and other one a lambda expression. Under the hood something must be happening .



If somebody can explain it and save me some of my black hairs from becoming white.

 
Mahtab Alam
Ranch Hand
Posts: 391
1
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was writing small programs to get a grasp of Lambda Expressions in Java.

I read this

Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.



about Functional interfaces in docs : Functional Interface

So it means lambda expressions provide shorter way to implement a functional interface.
But under the hood an instance of functional interface is created while using lambda expressions.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not believe that you can create instances of functional interfaces with λs. Try writing a little class with an anonymous class. Then compile it and list all the contents of the directory. It will read something like

Foo.class
Foo.java
Foo$1.class

So you have two .class files, one for Foo and the other for the anonymous class. Now delete all the .class files and change Foo to use a λ and compile again. You now only have one .class file (Foo.class) and no $1.class file.

Another way to think of it is that a one‑method interface can be instantiated as an anonymous class, and that anonymous class comprises a way to supply the functionality of that one method. Now a λ is a way to supply the functionality directly without requiring the anonymous class. It is probably unfortunate that anonymous classes have been around for a long time, so you cannot use an analogous technique to replace multiple‑method anonymous classes.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
…and how are you going to create an instance without a class to create it from?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try a situation where you can create anonymous classes and then retrieve references to them. Try a button and addActionListener and getActionListeners and print the result of the latter with Arrays.toString. See whether you are getting objects back. Tell us what happens.
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic