Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Lambdas are purely a call-site construct: the recipient of the lambda does not need to know that a Lambda is involved, instead it accepts an Interface with the appropriate method.</p> <p>In other words, you define or use a functional interface (i.e. an interface with a single method) that accepts and returns exactly what you want.</p> <p>For this Java 8 comes with a set of commonly-used interface types in <a href="http://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html" rel="noreferrer"><code>java.util.function</code></a> (thanks to <a href="https://stackoverflow.com/users/1859863/maurice-naftalin">Maurice Naftalin</a> for the hint about the JavaDoc).</p> <p>For this specific use case there's <a href="http://docs.oracle.com/javase/8/docs/api/java/util/function/IntBinaryOperator.html" rel="noreferrer"><code>java.util.function.IntBinaryOperator</code></a> with <a href="http://docs.oracle.com/javase/8/docs/api/java/util/function/IntBinaryOperator.html#applyAsInt-int-int-" rel="noreferrer">a single <code>int applyAsInt(int left, int right)</code> method</a>, so you could write your <code>method</code> like this:</p> <pre><code>static int method(IntBinaryOperator op){ return op.applyAsInt(5, 10); } </code></pre> <p>But you can just as well define your own interface and use it like this:</p> <pre><code>public interface TwoArgIntOperator { public int op(int a, int b); } //elsewhere: static int method(TwoArgIntOperator operator) { return operator.op(5, 10); } </code></pre> <p>Using your own interface has the advantage that you can have names that more clearly indicate the intent.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload