2

Netbeans always suggests that I should use Lambda expressions, and I know lambda is something new. So I'm wondering if I convert the code to use lambda expression, will it work if I export it as a jar and send it to other people also those who don't have the newest Java installed, and if not what will happen when they run it?

enter image description here

And when making school projects, I'm sending the netbeans project, and there's a chance that the one that will test my code don't have the newest version. Do you think I should use lambda expressions?

I do like that cleaner and simpler code, but I want it to work everywhere also.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Jeggy
  • 1,474
  • 1
  • 19
  • 35
  • you have to agree with the recipients of your binaries on a target version of the jvm then you can use whatever that vm supports. (note that you can setup your to compile for that target vm in which case your ide will not show the warnings in the first place) – A4L Feb 12 '15 at 18:13
  • 3
    If you compile your classes with JDK 8 and you use Java 8 features such as lambda expressions then it's only going to work if those other people are also using Java 8. They will not be able to use your classes if they use an older Java version. – Jesper Feb 12 '15 at 18:44

1 Answers1

2

If you are using it at school, that means that probably your teacher has Java 8 as well. Feel free to use lambda expressions if you feel that it makes your code shorter.

If you let Netbeans 8 convert that into a lambda expression, you'll get something like submitButton.addActionListener(e -> {doStuff();}). This is functionally equivalent. Literally by e -> {doStuff();} you mean "make me an object of a class with function of e that matches the argument type, ActionEvent, and has the body doStuff."

Lambda expressions are an extremely powerful (and cool) functional programming API in Java 8.

Simon Kuang
  • 3,870
  • 4
  • 27
  • 53
  • When I took a computer science class, I had the latest Java and Netbeans on my PC but my teacher and the other school machines did not, so I had to revert to the old-fashioned syntax. – Simon Kuang Feb 12 '15 at 18:16
  • 1
    It's not functionally equivalent in some minor ways (e.g. `new` guarantees to create a new object, whereas lambda could create a new object or return an existing object) that are not important most of the time. – newacct Feb 13 '15 at 00:38
  • @newacct So like `String`s, basically. – Simon Kuang Feb 13 '15 at 21:22