Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Java syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Functional interfaces and lambda expressions==== These features were introduced with the release of Java SE 8. An interface automatically becomes a functional interface if it defines only one method. In this case an implementation can be represented as a lambda expression instead of implementing it in a new class, thus greatly simplifying writing code in the [[functional programming|functional style]]. Functional interfaces can optionally be annotated with the <code>[[@FunctionalInterface]]</code> annotation, which will tell the compiler to check whether the interface actually conforms to a definition of a functional interface. <syntaxhighlight lang="java"> // A functional interface @FunctionalInterface interface Calculation { int calculate(int someNumber, int someOtherNumber); } // A method which accepts this interface as a parameter int runCalculation(Calculation calculation) { return calculation.calculate(1, 2); } // Using a lambda to call the method runCalculation((number, otherNumber) -> number + otherNumber); // Equivalent code which uses an anonymous class instead runCalculation(new Calculation() { @Override public int calculate(int someNumber, int someOtherNumber) { return someNumber + someOtherNumber; } }) </syntaxhighlight> Lambda's parameters types do not have to be fully specified and can be inferred from the interface it implements. Lambda's body can be written without a body block and a <code>return</code> statement if it is only an expression. Also, for those interfaces which only have a single parameter in the method, round brackets can be omitted.<ref>{{Cite web|title=Lambda Expressions (The Javaβ’ Tutorials > Learning the Java Language > Classes and Objects)|url=https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html|access-date=August 8, 2021|website=docs.oracle.com|archive-date=June 16, 2020|archive-url=https://web.archive.org/web/20200616055353/https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html|url-status=live}}</ref> <syntaxhighlight lang="java"> // Same call as above, but with fully specified types and a body block runCalculation((int number, int otherNumber) -> { return number + otherNumber; }); // A functional interface with a method which has only a single parameter interface StringExtender { String extendString(String input); } // Initializing a variable of this type by using a lambda StringExtender extender = input -> input + " Extended"; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)