Forums Register Login

Lambda Expressions (The difference between)

+Pie Number of slices to send: Send
Hi Folks,

This is my first day in Lambda world.

And I cannot understand why this code works:
new JButton().addActionListener(e -> {System.err.println("Hello from lambda");});

And this doesn't:
new Thread(r -> {System.err.println("Hello from lambda");}).start();

What is the difference???

Thanks!

Omar
+Pie Number of slices to send: Send
If I understand lambda expressions correctly, the e in the action listener is the argument to the actionPerformed method. The r would then be the argument to the run method, but it doesn't take any arguments. Check out http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#target-types-and-method-arguments on how to do this.
+Pie Number of slices to send: Send
Hi Omar,

The point here is what 'e' is (and what would be 'r' in that case). The 'e' would be the ActionListener method's parameter, so you could use it in your Lambda Expression. In the Runnable interface, the method 'run' doesn't have parameters, so only '()' identifier would be allowed. Below some documentation of Lambda Expression (JSR 335) that may help:

http://cr.openjdk.java.net/~dlsmith/jsr335-0.6.1/B.html
http://download.oracle.com/otndocs/jcp/lambda-0_6_1-edr3-spec/index.html

Best Regards! =)

Luan
+Pie Number of slices to send: Send
Hi guys!

Of course it's the method argument... How couldn't I see that
Thank you guys and have a great weekend.

Kind Regards
Omar
2
+Pie Number of slices to send: Send
Actually, you don't even need the braces:

Look! It's Leonardo da Vinci! And he brought a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1352 times.
Similar Threads
Exception: Connection refused to host
OCPJP6 OR OCPJP7
Calling method in Java App from Jython
Exception Handling Code Doesn't Work
So is it still Java?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:07:13.