0

I just started learning java8 stream api. I have seen a method which has input type Runnable interface even it's allow to pass this::greet as param. when program is run, why does this not call greet method? its output only Hello, world!2. why does it allow to pass such method even input is runnable interface?

public class TestAnno {

    public static void main(String[] args) {
        TestAnno anno=new TestAnno();
    }
    public TestAnno() {
        display(this::greet); // what is use of calling this method
        display(this.greet()); //compiler error: The method display(Runnable) in the type TestAnno is not applicable for the arguments (void)
    }
    private  void display(Runnable s) {
        System.out.println("Hello, world!2");
        //Arrays.sort(new String[]{"1","2"}, String::compareToIgnoreCase);

    }
    public void greet() {
        System.out.println("Hello, world! greet");

    }
}

I have created interface to understand it.

public interface Inter1 {
 void hello();
 void hello1(int a);
}

Now I change display method param to Inter1 instead of Runnable. it throw error 'The target type of this expression must be a functional interface'.

like

public class TestAnno {

    public static void main(String[] args) {
        TestAnno anno=new TestAnno();
    }
    public TestAnno() {
        display(this::greet); // The method display(Inter1) in the type TestAnno is not applicable for the arguments (this::greet)
        display(()->this.greet());//The target type of this expression must be a functional interface

    }
    /*private  void display(Runnable s) {
        System.out.println("Hello, world!2");
        Arrays.sort(new String[]{"1","2"}, String::compareToIgnoreCase);

    }*/
    private  void display(Inter1 s){

    }
    public void greet() {
        System.out.println("Hello, world! greet");

    }


}

Can any one help on this!!

bNd
  • 7,512
  • 7
  • 39
  • 72
  • What do you expect the output to be? – sdgfsdh Jan 06 '16 at 10:23
  • I don't understand why it's not showing error. If I want to use such concept again for any class design, what things are required in my interface i.e.`Runnable`, any other custom interface. or a method param should have? – bNd Jan 06 '16 at 10:26
  • You can pass in place of a `Runnable` any method that takes no arguments and returns `void`. – sdgfsdh Jan 06 '16 at 10:37
  • @sdgfsdh updated question. create a seperate interface which has two method. even it does not call empty one. and throw compilation error. – bNd Jan 06 '16 at 10:39

1 Answers1

1

this::greet refers to a method with no parameters and no return value. As such, it matches the single method of the Runnable interface - void run().

Your display method accepts a Runnable instance. It must execute that's Runnable's run method in order for your greet method to be executed :

private  void display(Runnable s) {
    s.run();
    System.out.println("Hello, world!2");
}

Your second attempt to call display can pass compilation if you turn it into a lambda expression :

display(()->this.greet());
Eran
  • 387,369
  • 54
  • 702
  • 768
  • thanks. I understood. :) If I have two method with no paramter, it throws error. is there any way to deal with? – bNd Jan 06 '16 at 10:30
  • @bmt What do you mean by that? What two methods? Please be more specific. – Eran Jan 06 '16 at 10:34
  • @bmt A functional interface can only have a single abstract method. `Inter1` doesn't work, since it contains two methods. – Eran Jan 06 '16 at 10:41
  • thanks for your input. its really helps.!! – bNd Jan 06 '16 at 10:46