Sunday, February 22, 2015

2-22-15

JAVA 1.8:

-Interfaces

-learning lambda

Will revisit this.......

Working some more on the todo list app...

------------------------------------------------------------
Interface

-with java 1.8 you can have a static method with a body on the interface

-you can have a default method with a body on the interface

like in this example:


package com.company;

/** * Created by fadi on 2/22/15. */public interface IReturn {

    public default int returnInt(int x) {
        return x;
    }

    public static boolean isIReturn(Object obj) {

        return obj instanceof IReturn;
    }



}


package com.company;

public class Main implements IReturn {

    public static void main(String[] args) {

        System.out.println("Hello World this is java 1.8");


        Main main = new Main();

        System.out.println(IReturn.isIReturn(new Object()));
        System.out.println(IReturn.isIReturn(main));
        System.out.println(main.returnInt(119));
    }


//    public int returnInt(int x){////        return x = 10;//    }}

when you run this you get the following:


Hello World this is java 1.8
false
true
119

What is Lambda?

-Lambda is a function

-Function is a process that takes parameters and returns a value

-In the past functions can only be implemented by using methods, java changed that.

-Lambda lets you treat functions like data; they can be stored or passed around

-Lambda enables you to pass a function as a parameter to a different function directly; this was not possible before java 8. 


Here is a simple example:

we have a main


package com.company;

public class Main implements IReturn {

    public static void main(String[] args) {

        System.out.println("Hello World this is java 1.8");


       //Main main = new Main();        IReturn iret = (int x) -> { return x * x; };


        //System.out.println(IReturn.isIReturn(new Object()));
        System.out.println(iret.returnInt(10));
        new Thread(thread).start();
    }


    public int returnInt(int x){

        return x + 2;
    }
}

and an interface with a abstract method returnInt


public interface IReturn {

    public abstract int returnInt(int x);


    public static boolean isIReturn(Object obj) {

        return obj instanceof IReturn;
    }



}

the output for this would be:


Hello World this is java 1.8
100

instead of making a separate class and a method to return x * x we just used Lambda ->  make an instance of the interface iret then x -> x * x as seen in the example above you get a return value of x = x * x as shown.  

Another example: Creating a thread.


Runnable thread = () -> { System.out.println("This is a thread"); };

System.out.println(iret.returnInt(10));
new Thread(thread).start();


Runnable is just an interface and Thread is a class, similar to the last example. 

Another example; if you want to round a double to an integer using the Function interface and its apply abstract method do the following:


unction<Double, Integer> func = (Double d) -> { return (int) Math.round(d); };

System.out.println(func.apply(Math.PI));


This will round the Double PI into an integer.

Another example do some math with the same interface do the following:


Function<Double, Double> func = (Double x) -> { return ((x * x) + (x / 100) + 1500); };

System.out.println(func.apply(100d));


This will do the following returns x = (100 * 100) + (100 /100) +1500 = 11501.0


So Lambda will make your programing life much easier if you understand it well it will save you from writing alot of code. i will revisit this.....

Here is the full example listing:

import java.util.function.Function;

public class Main implements IReturn {

    public static void main(String[] args) {

        System.out.println("Hello World this is java 1.8");


       //Main main = new Main();        IReturn iret = (int x) -> { return x * x; };

        Runnable thread = () -> { System.out.println("This is a thread"); };

        //Function<Double, Integer> func = (Double d) -> { return (int) Math.round(d); };
        Function<Double, Double> func = (Double x) -> { return ((x * x) + (x / 100) + 1500); };


        //System.out.println(IReturn.isIReturn(new Object()));
        System.out.println(iret.returnInt(10));
        new Thread(thread).start();

        //System.out.println(func.apply(Math.PI));
        System.out.println(func.apply(100d));
    }


    public int returnInt(int x){

        return x + 2;
    }
}



package com.company;

/** * Created by fadi on 2/22/15. */

public interface IReturn {

    public abstract int returnInt(int x);


    public static boolean isIReturn(Object obj) {

        return obj instanceof IReturn;
    }

 









No comments:

Post a Comment