Return value by lambda in Java

后端 未结 3 1909
遥遥无期
遥遥无期 2021-02-13 15:24

Till now I manage to find all answers I need but this one confusing me. Let\'s say we have example code:

public class Animal {
   private String species;
   priv         


        
3条回答
  •  遥遥无期
    2021-02-13 15:40

    Does this mean that, behind the scenes, Java adds keyword return to code in the first case?

    No, The compiler generates byte code, and it might generate the same byte code but it doesn't change the syntax and then compile it again.

    we wanted to ignore boolean return type of method.

    It has the option of ignoring a value based on what functional interfaces it is considering.

    a -> a.canHop()
    

    could be

    (Animal a) -> { return a.canHop(); }
    

    or

    (Animal a) -> { a.canHop(); }
    

    based on context, however it favours the first if possible.

    Consider ExecutorService.submit(Callable) and ExecutorService.submit(Runnable)

    ExecutorService es = Executors.newSingleThreadExecutor();
    es.execute(() -> counter++); // has to be Runnable
    es.submit(() -> counter++); // Callable or Runnable?
    

    Saving the return type you can see it's a Callable

    final Future submit = es.submit(() -> counter++);
    

    To try yourself, here is a longer example.

    static int counter = 0;
    
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService es = Executors.newSingleThreadExecutor();
    
        // execute only takes Runnable
        es.execute(() -> counter++);
    
        // force the lambda to be Runnable
        final Future submit = es.submit((Runnable) () -> counter++);
        System.out.println(submit.get());
    
        // returns a value so it's a Callable
        final Future submit2 = es.submit(() -> counter++);
        System.out.println(submit2.get());
    
        // returns nothing so it must be Runnable
        final Future submit3 = es.submit(() -> System.out.println("counter: " + counter));
        System.out.println(submit3.get());
    
        es.shutdown();
    }
    

    prints

    null
    2
    counter: 3
    null
    

    The first submit take a Runnable so Future.get() returns null

    The second submit defaults to being a Callable so Future.get() returns 2

    The third submit can only be a void return value so it must be a Runnable so Future.get() returns null

提交回复
热议问题