-1

Can I use Java 8 method references, such as myInstance::myMethod, interchangeably with a lambda, like () -> myInstance.myMethod(), or are there any caveats on how they are created/evaluated/executed and sometimes this can give way to different runtime behaviors and particularities?

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • 3
    https://stackoverflow.com/questions/51622262/function-pointer-to-string-method-in-java – Oleksandr Pyrohov Oct 31 '18 at 15:16
  • 2
    Note: it's `myInstance::myMethod`, not `myInstance::myMethod()`. – Thomas Oct 31 '18 at 15:17
  • oh yeah haha, that's what you get when you go YOLO withouth the IDE lol – Michel Feinstein Oct 31 '18 at 15:21
  • 1
    See [What is the equivalent lambda expression for System.out::println](https://stackoverflow.com/a/28025717/2711488); in short, you can always write an equivalent lambda expression, but the exact equivalent is not always that simple. – Holger Oct 31 '18 at 15:23
  • So, basically the ONLY difference between them is that a method reference will store the instance variable (`myInstance`) at the moment it was created...and a lambda will reevaluate the instance variable (`myInstance`) at all executions? – Michel Feinstein Oct 31 '18 at 15:27
  • There are other differences. A lambda is actually a kind of anonymous inner class; a method reference is not. – VGR Oct 31 '18 at 15:28
  • Could you answer the question with all the differences, specially the ones that can trigger different side effects? I can't find it all compiled in one place – Michel Feinstein Oct 31 '18 at 15:37
  • 1
    @mFeinstein That is not a difference, since both evaluates the `myInstance` reference value at the time the lambda / method-reference is defined. If you're looking for a difference, the lambda requires `myInstance` to be *effectively final*, while a method reference doesn't. – Andreas Oct 31 '18 at 15:44

1 Answers1

0

I would strongly recommend that you not conflate method references with lambdas. Lambdas are their own topic of discussion and are not wholly dependent on method references.

It just so happens that, through method references, we can remove a lot of boilerplate because method references provide syntactic sugar.

For example, suppose I wanted a function which took two parameters of different types (say a String and an Integer) and I wanted it to output a String.

BiFunction<String, Integer, String> func = (s, i) -> s.substring(i);

I could rewrite this with method references instead, since a method exists that fits this signature.

BiFunction<String, Integer, String> func = String::substring;

If I wanted to fit the other signature I'd have to create my own @FunctionalInterface to accomplish this.

@FunctionalInterface
interface TriFunction<T, U, V, R> {
    R apply(T t, U u, V v);
}

Method references just reduce the boilerplate of working with lambdas. If a method fits the signature of your lambda, you can use it in place of the lambda expression.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • I am asking this question precisely because I got different behavior from converting a lambda to a method reference. So I don't think it's as simple as "if the signature fit they will behave the same" – Michel Feinstein Oct 31 '18 at 15:52
  • Um...that's not how lambdas work. How about this: write a *new* question which illustrates the two pieces of code together and explains what different response you're getting from turning a lambda into a method reference. – Makoto Oct 31 '18 at 16:13
  • I want to, but I am afraid the code might be too big, since it's an Android `LiveData`, inside a `Fragment`, that I converted to an `Event` which is triggered by a `ViewModel`...and the different behavior happens when Android goes back to the same `Fragment`...so I am having a hard time simplifying it for a question – Michel Feinstein Oct 31 '18 at 16:14
  • You're going to have to simplify since in broad strokes no one here is lying to you but it really isn't the answer you seek. – Makoto Oct 31 '18 at 16:15
  • Yeah, I will try to upload something to github at least....thanks anyways – Michel Feinstein Oct 31 '18 at 16:16