0

I am trying to understand this example code from Oracle Learning on Lambdas and Method References:

String city = "Munich";
Supplier<String> lambda = city::toUpperCase;
System.out.println(lambda.get());

Why didn't they simply call

city.toUpperCase();

Isn't this method tied to the specific instance variable city? So how would it execute in a different context to provide the benefits of lambdas - I am unable to understand that.

likejudo
  • 3,396
  • 6
  • 52
  • 107
  • 1
    Shouldn’t the question be, why didn’t they just write `"MUNICH"` in the first place? – Holger Apr 23 '21 at 06:20
  • @Holger you are welcome to ask that question. – likejudo Apr 23 '21 at 14:36
  • 1
    I thought, it helps understanding the principle of an *example*. In the example, the string is `"Munich"` and you could write `"MUNICH"` in the first place. But when you have an *arbitrary* string, you can convert it to uppercase by calling `city.toUpperCase()`. Likewise, in the example, the supplier is an invocation of `toUpperCase` on `city` and you could write `city.toUpperCase()` or `"MUNICH"` in the first place. But when you have an *arbitrary* supplier, you can request a result by calling `get()`. Consider what happens when a method’s parameter is `String city` or `Supplier s`… – Holger Apr 23 '21 at 14:52

2 Answers2

4

In that limited code snippet you show, you would indeed just call city.toUpperCase();. There is no point in using a lambda there.

You must be ignoring a larger lesson. I suspect the author of that tutorial code was demonstrating the effect of such code, explaining the equivalent behavior. You should link to the exact tutorial page for greater context.

The point of a lambda is that you want to execute that method reference elsewhere in the code base. Rather than immediately execute that method in the current code, you want some other context of code to run that method. You want to pass that method reference as an argument to some other method call.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 2
    Yea. The tutorial page is providing a simple example to illustrate "how to do it". Not an example to illustrate "when you **should** do it". – Stephen C Apr 23 '21 at 03:06
  • @BasilBourque Isn't this method tied to the specific instance variable `city`? So how would it execute in a different context - I am unable to understand that. The video tutorial is available for free on learn.oracle.com Java SE 11 training until April 25 - it is behind a paywall so no public links are possible. – likejudo Apr 23 '21 at 14:41
  • Course title is: *Prepare For Java SE 11 Certification* Topic 4 'Lambdas and Streams Part 2'. Free access until April 25. – likejudo Apr 23 '21 at 14:47
  • 1
    @likejudo See, for example [Objects.requireNonNull(T obj, Supplier messageSupplier)](https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html#requireNonNull-T-java.util.function.Supplier-). The method will only invoke `messageSupplier.get()` when the `obj` is `null` and a message to construct a `NullPointerException` is needed. Otherwise, it isn’t executed at all, which might be better than constructing a string beforehand that turn out to be not needed (as the typical case should be that an input is not `null` and no exception be thrown). – Holger Apr 23 '21 at 14:56
0

Because then it wouldn't be a good example for "Learning on Lambdas and Method References" ...

dominicoder
  • 9,338
  • 1
  • 26
  • 32