Page 1 of 1

About Question enthuware.ocpjp.v8.2.1766 :

Posted: Mon Sep 23, 2019 10:56 pm
by sreeharshitha
a Supplier interface takes no input but returns a value. In this, name::toUpperCase, will take name="bob" as input right?

Re: About Question enthuware.ocpjp.v8.2.1766 :

Posted: Mon Sep 23, 2019 11:42 pm
by admin
When we say that a functional interface takes no input, it actually means that the functional method of that functional interface takes no input.

So, no, there is no input to the toUpperCase method. It is invoked on the String reference. Think of it like this:

Code: Select all

String name = "bob";
Supplier<String> s = new Supplier(){
      String get(){
         return name.toUpperCase();
      }
  };
The above is what Supplier<String> s = name::toUpperCase; is translated into.

Re: About Question enthuware.ocpjp.v8.2.1766 :

Posted: Sat Dec 16, 2023 2:32 am
by thienday
Hi admin, can you write a topic about the method reference in Java. When i have to face it i have some conclusion like this :
If it use the variable to get method like this example. When run time it compile to lambda like variable.method(name.toUpercase)
If it use the class and use the instance method like String::isEmpty. When run time it compile to lambda like parameter.method(s -> s.isEmpty())
If it use static method like Class::staticmethod When run time it compile to lambda like Class.staticmethod(parameter)

Re: About Question enthuware.ocpjp.v8.2.1766 :

Posted: Thu Dec 21, 2023 12:48 pm
by yulinxp
Check out here https://javadevcentral.com/java-method-references The best of best...