• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 8 Lambda Expressions  RSS feed

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • 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
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • 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.

 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and how are you going to create an instance without a class to create it from?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!