• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why can I write () -> something?  RSS feed

 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for your quick answers. One more question though:

Why does this work: SwingUtilities.invokeLater( () -> startApplication() ); ??

SwingUtilities.invokeLater takes a runnable into its parameters but startApplication is not a Runnable. why does it work when using () -> ??

Thanks again guys.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your new question is big enough to merit a thread of its own, so I have created a new thread for it.
In the good old days (up to Java7) we would write a class implementing the Runnable interface, maybe a package‑private class:- ...and in (I think) JDK1.2 they introduced nested classes, so we would often write that as an anonymous class:-Because you are inside the class, you can miss out new Application().
Now, you are writing nine lines of code when you could get away with one line. Who wants all that code, if it is actually boiler‑plate, and the compiler can actually impute most of it? In Java8, they introduced the λ expression, which you can find out about in the tutorials link above. You can make () -> startApplication() into a Runnable if you have a bit more information.
  • 1: The invokeLater method takes a Runnable as its parameter. So the compiler knows it is trying to create a Runnable.
  • 2: Runnable is a FunctionalInterface. That means it has one “abstract” method. If the compiler creates a Runnable, it doesn't have to guess which method to implement, because there only is one: run().
  • 3: The run() method is not overloaded. I think you couldn't call an interface functional if it had an overloaded method.
  • The compiler “knows” it has to create an instance of something implementing Runnable, and that there is only one method to implement. So let's see how much information it actually requires:-So we have managed to reduce that to the list of parameters, and the one line which is the method body. Everything else will be the same in all instances, but the method body and the list of parameters might be different. So you need to keep those.Now, deleting the commented‑out code and some whitespace and the semicolon on line& leaves us with:-Now, all you have to do is join the two halves of the method call together with the -> operator and Bob's your uncle The right hand half of line 2 is a λ expression, which supplies all the information the compiler needs to assemble a Runnable object.
    The syntax is different if you have multiple lines in the method body, or if you have different parameter lists.
     
    Bart Boersma
    Ranch Hand
    Posts: 52
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you. Very clear explanation!
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's a pleasure Did you find the Java™ Tutorials link helpful?

    When I wrote semicolon on line& that shou‍ld have read semicolon on line7.
     
    Paweł Baczyński
    Bartender
    Posts: 2074
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:

    Let's go one step further.
    The compiler knows that the run() method does not need any parameters so you can skip this too using a method reference.

    The code becomes (assuming startApplication() is not static):
    or (assuming startApplication() is static and the class name is Example):
     
    Bart Boersma
    Ranch Hand
    Posts: 52
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I didnt read all of it, but in general these are very useful yes.

    Thanks again guys for the explanation.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!