Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SwingUtilities.invokeLater( Runnable ) and lambdas  RSS feed

 
Carey Brown
Bartender
Posts: 2984
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are these the same?


Vs...

 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • 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.
 
Liutauras Vilda
Marshal
Posts: 4633
316
BSD
  • Mark post as helpful
  • send pies
  • 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>".
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 2984
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Darryl, that was useful reading.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I quite forgot about ::new.

Remind me, Jesper, please, does ::new always mean the no‑args constructor?
 
Stephan van Hulst
Saloon Keeper
Posts: 7803
142
  • Mark post as helpful
  • send pies
  • 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: 55681
161
  • Mark post as helpful
  • send pies
  • 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: 7803
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Stephan van Hulst
Saloon Keeper
Posts: 7803
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Than kyou again
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 55681
161
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!