3

What is better in calling

names.stream().forEach(System.out::println);

Than

names.stream().forEach(n -> System.out.println(n));

Despite the fact you have to write less code? Are there any other advantages of introducing method references in Java 8?

pppery
  • 3,731
  • 22
  • 33
  • 46
Dariusz Mydlarz
  • 2,940
  • 6
  • 31
  • 59
  • 3
    Despite the fact you have to write less code are there any advantages of lambdas? – Boris the Spider Jan 25 '15 at 11:54
  • 1
    Note that there are plenty of examples where the method reference actually means *more* code (e.g. `MyClassWithLongName::aStaticMethod`). The method reference can still be an advantage even there because it gives less opportunities for something to go wrong. – Marko Topolnik Jan 25 '15 at 12:10
  • 5
    The motivation for method references is simple (and does not have nearly as much to do with "shorter" as many people assume.) Methods have _names_, and referring to something by its name is often clearer than the alternative. – Brian Goetz Jan 25 '15 at 18:26
  • Another useful thing about method references is that they conform by default to the java.util.function functional interfaces, so you can use a reference to your method for things such as a Predicate or Supplier. – Steve K Jan 28 '15 at 04:06

2 Answers2

6

Despite the fact you have to write less code? Are there any other advantages of introducing method references in Java 8?

Having to write less code is enough of an advantage to consider introducing a language feature. There is a similar feature in C#, called method groups, that makes it easier to write code that uses delegates. Lambdas are shorthand for anonymous classes, and anonymous classes could be considered shorthand for named classes. One could legitimately call them "syntactic sugar", yet all these features help you write less code.

In addition to letting you shorten the code, the feature has a potential of helping designers of Java compiler generate more efficient code. For example, it might be possible to avoid generating a brand-new type for each lambda wrapping a method reference.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 5
    Further advantages I would name: method reference offers a feel of the "point-free" programming style; a named parameter is not just more characters, it incurs a cognitive load in that it requires us to carefully follow it through to each dereferencing; that also opens the doors to bugs (an unrelated, similar-named variable may be dereferenced in the body); a method reference is a clear signal that the lambda is not a closure. – Marko Topolnik Jan 25 '15 at 12:06
  • 1
    In other words, if you see an expression like `(a,b,c,d)->a.foo(b,d,c)` you have to read it with more care than an expression like `MyClass::foo` (otherwise you might miss that they are *not* equivalent…). And the method reference indicates the declaring class of the target without the need to declare all parameter types at the same time. – Holger Jan 26 '15 at 08:45
0

Well, one advantage above just writing less code is, that the first example doesn't have to be aware of the parameters, as long as the implementation of that functional interface has the same signature (-> is correct) your code get's compiled.

If the signature of the the functional interface changes and the signature of the implementation changes analogously these changes only need to be applied there and not on that glue code.

Ronald Duck
  • 323
  • 1
  • 11