Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Usually, one would call the <code>reduce</code> method using <code>Math.max(int, int)</code> as follows:</p> <pre><code>reduce(new IntBinaryOperator() { int applyAsInt(int left, int right) { return Math.max(left, right); } }); </code></pre> <p>That requires a lot of syntax for just calling <code>Math.max</code>. That's where lambda expressions come into play. Since Java 8 it is allowed to do the same thing in a much shorter way:</p> <pre><code>reduce((int left, int right) -&gt; Math.max(left, right)); </code></pre> <p>How does this work? The java compiler "detects", that you want to implement a method that accepts two <code>int</code>s and returns one <code>int</code>. This is equivalent to the formal parameters of the one and only method of interface <code>IntBinaryOperator</code> (the parameter of method <code>reduce</code> you want to call). So the compiler does the rest for you - it just assumes you want to implement <code>IntBinaryOperator</code>.</p> <p>But as <code>Math.max(int, int)</code> itself fulfills the formal requirements of <code>IntBinaryOperator</code>, it can be used directly. Because Java 7 does not have any syntax that allows a method itself to be passed as an argument (you can only pass method results, but never method references), the <code>::</code> syntax was introduced in Java 8 to reference methods:</p> <pre><code>reduce(Math::max); </code></pre> <p>Note that this will be interpreted by the compiler, not by the JVM at runtime! Although it produces different bytecodes for all three code snippets, they are semantically equal, so the last two can be considered to be short (and probably more efficient) versions of the <code>IntBinaryOperator</code> implementation above!</p> <p>(See also <a href="http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html" rel="noreferrer">Translation of Lambda Expressions</a>)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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