Loading
 
What's Next: View All Tutorial →

Java 8 - Double operator ::

Using :: Double operator we can access static method, class method and the constructure  A simple program is give with the example ::Double Operator can be used if the Interface has only ...

Published Date: 07/09/2018  Last Modified Date: 07/09/2018    New Post




Using :: Double operator we can access static method, class method and the constructure 

A simple program is give with the example

::Double Operator can be used if the Interface has only one method. Define anotation @FunctionalInterface is optional

 

public class DoubleOperator {

    public static void main(String args[]) {

        //Concept 1 to access static method
        CalculatorAddFunc cf = Calculator::add;
        cf.add(1, 2);

        //Concept 2 to access method using class instance
        CalculatorDivideFunc cf1 = new Calculator()::divide;
        cf1.divide(1, 2);

        //Concept 3 to create instance of class using double operator
        CalculatorNewObjectFunc cf2 = Calculator::new;
        cf2.getRef().divide(3, 4);

    }
}


//Concept 1: Refering static method
@FunctionalInterface
interface CalculatorAddFunc {

    public int add(int x, int y);
}

//Concept 2: Refering class method
@FunctionalInterface
interface CalculatorDivideFunc {

    public double divide(int x, int y);
}

//concept 3: Refering constructor
@FunctionalInterface
interface CalculatorNewObjectFunc {

    public Calculator getRef();
}


class Calculator {

    public static int add(int x, int y) {
        return x + y;
    }

    public double divide(int x, int y) {
        return x / y;
    }
}



 
Awaiting for Administrator approval




← Back to list


Related Post




×