• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

SwingUtilities.invokeLater( Runnable ) and lambdas

 
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are these the same?


Vs...

 
Marshal
Posts: 74034
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes and no.

If you execute the two you will not notice any difference.
If you explore the directory your .class files are in, there will be a $1 file for the Runnable class in one case but not in the other. You could consider exploring the bytecode for that method and see what difference that makes to the bytecode.
 
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You could consider exploring the bytecode for that method and see what difference that makes to the bytecode.

And that is "javap -c <class>".
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey, you may be interested to read my post on Swing and Java 8, which covers this and other uses for the new language features in Swing programming.
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Darryl, that was useful reading.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a third way to write this:

This is a method reference, or in this case a reference to the no-args constructor of BouncingBalls.
 
Campbell Ritchie
Marshal
Posts: 74034
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I quite forgot about ::new.

Remind me, Jesper, please, does ::new always mean the no‑args constructor?
 
Saloon Keeper
Posts: 13269
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, like how System.out::println doesn't mean the no-args println() method. Which method is called depends on what arguments the functional interface expects. invokeLater expects a Runnable, which is () -> void. If the return type is void, you may use a method reference with any return type, as long as the parameters match.

If you have a method that takes an Elastic -> BouncingBalls, you can pass it BouncingBalls::new, if BouncingBalls has a constructor that accepts Elastic.
 
Campbell Ritchie
Marshal
Posts: 74034
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if you have a λ or a Stream<Foo> passing a method reference implicitly takes the type of the Stream as the type of argument to the method reference?
 
Stephan van Hulst
Saloon Keeper
Posts: 13269
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
 
Campbell Ritchie
Marshal
Posts: 74034
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Stephan van Hulst
Saloon Keeper
Posts: 13269
292
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's look at a concrete example:
Say we have a Stream<Integer> and we want to use the reduce method to create a single string with all the integers concatenated. You could pass method references to it like so:
 
Campbell Ritchie
Marshal
Posts: 74034
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Than kyou again
 
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From a presentation I gave at work yesterday*:
(Don't mind the duplicate declaration of f).


* We've only started using Java 8 recently after the first JBoss 6.4.x project was started, and Java 8 was finally officially supported. Personally I've been using it for over a year already, so I was the likely candidate for showing everybody the ropes.
 
Campbell Ritchie
Marshal
Posts: 74034
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every time I see such Java8 code, I realise there are yet more things you can do which I had never thought of.
Thank you Rob.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic