Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While this is not yet valid for Java 7 and below, I believe that we should look to the future and at least recognize <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html" rel="nofollow">the changes</a> to come in new versions such as Java 8. </p> <p>Namely, this new version brings <a href="http://www.lambdafaq.org/what-is-a-lambda-expression/" rel="nofollow">lambdas</a> and method references to Java (along with <a href="http://lambdadoc.net/api/" rel="nofollow">new APIs</a>, which are another valid solution to this problem. While they still require an interface no new objects are created, and extra classfiles need not pollute output directories due to different handling by the JVM. </p> <p>Both flavors(lambda and method reference) require an interface available with a single method whose signature is used:</p> <pre><code>public interface NewVersionTest{ String returnAString(Object oIn, String str); } </code></pre> <p>Names of methods will not matter from here on. Where a lambda is accepted, a method reference is as well. For example, to use our signature here:</p> <pre><code>public static void printOutput(NewVersionTest t, Object o, String s){ System.out.println(t.returnAString(o, s)); } </code></pre> <p>This is just a simple interface invocation, up until the lambda<sup>1</sup> gets passed:</p> <pre><code>public static void main(String[] args){ printOutput( (Object oIn, String sIn) -&gt; { System.out.println("Lambda reached!"); return "lambda return"; } ); } </code></pre> <p>This will output:</p> <pre><code>Lambda reached! lambda return </code></pre> <p>Method references are similar. Given:</p> <pre><code>public class HelperClass{ public static String testOtherSig(Object o, String s){ return "real static method"; } } </code></pre> <p>and main:</p> <pre><code>public static void main(String[] args){ printOutput(HelperClass::testOtherSig); } </code></pre> <p>the output would be <code>real static method</code>. <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html" rel="nofollow">Method references can be static, instance, non-static with arbitrary instances, and even constructors</a>. For the constructor something akin to <code>ClassName::new</code> would be used.</p> <p><sup>1</sup> This is not considered a lambda by some, as it has side effects. It does illustrate, however, the use of one in a more straightforward-to-visualize fashion.</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. This table or related slice is empty.
    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