OCJP Exam watch tip #15

Capture

Prior to Java 8, as Java programmers we are used to writing anonymous inner classes. Let us see an example of writing equivalent Lambda fucntion for the below anonymous class

//AnonymousInnerClass.java

interface Function {
   void call();
}
class AnonymousInnerClass {
    public static void main(String []args) {
        Function function = new Function() {
            public void call() {
                System.out.println(“Hello world”);
           }
        };
        function.call();
    }
}

 

//EquivalentLambda.java

interface LambdaFunction {
    void call();
}
class EquivalentLambda{
    public static void main(String []args) {
        LambdaFunction lambdaFunction = () -> System.out.println(“Hello world”);
        lambdaFunction.call();
    }
}

Go through this article for further reading on Java 8: Lambda Functions – Usage & Examples

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s