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!
====Method references==== It is not necessary to use lambdas when there already is a named method compatible with the interface. This method can be passed instead of a lambda using a method reference. There are several types of method references: {| class="wikitable" |- ! Reference type !! Example !! Equivalent lambda |- | Static || <code>Integer::sum</code> || <code>(number, otherNumber) -> number + otherNumber</code> |- | Bound || <code>"LongString"::substring</code> || <code>index -> "LongString".substring(index)</code> |- | Unbound || <code>String::isEmpty</code> || <code>string -> string.isEmpty()</code> |- | Class constructor || <code>ArrayList<String>::new</code> || <code>capacity -> new ArrayList<String>(capacity)</code> |- | Array constructor || <code>String[]::new</code> || <code>size -> new String[size]</code> |} The code above which calls <code>runCalculation</code> could be replaced with the following using the method references: <syntaxhighlight lang="java> runCalculation(Integer::sum); </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)