• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between "->" and "::" ?

 
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was following the tutorial here:
https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html

QUESTION: Am I to understand that -> and :: are the same?

Notice that the interface Comparator is a functional interface. Therefore, you could use a lambda expression instead of defining and then creating a new instance of a class that implements Comparator:

Arrays.sort(rosterAsArray,
   (Person a, Person b) -> {
       return a.getBirthday().compareTo(b.getBirthday());
   }
);

However, this method to compare the birth dates of two Person instances already exists as Person.compareByAge. You can invoke this method instead in the body of the lambda expression:

Arrays.sort(rosterAsArray,
   (a, b) -> Person.compareByAge(a, b)
);

Because this lambda expression invokes an existing method, you can use a method reference instead of a lambda expression:

Arrays.sort(rosterAsArray, Person::compareByAge);

The method reference Person::compareByAge is semantically the same as the lambda expression (a, b) -> Person.compareByAge(a, b).
Each has the following characteristics:
   Its formal parameter list is copied from Comparator<Person>.compare, which is (Person, Person).
   Its body calls the method Person.compareByAge.

I am posting the codes from the Tute for your convenience:



Here is the Person class, which looks like the Model to me (something I picked up reading about MVC and MVP and AM-MVC, all very confusing). I can see my collection of classes expanding by a factor of 3.




QUESTION:  Can you write this code using a lambda to replace the method reference please?


 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
AhFai Chan
Ranch Hand
Posts: 271
1
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Zemiak
Thanks, it's much clearer now what the "::" does.
After I got the hang of what "->" actually does...
And it's actually in my highlighted text at the top... so, reconfirmed.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

AhFai Chan wrote:QUESTION: Am I to understand that -> and :: are the same?


Beware, "->" and "::" are not equivalent. For instance, if you need a Timer in a Swing program, then you must do something like

Suppose you have this code:

Why does this not compile? And how would you fix this?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you go through the Java™ Tutorials, particularly this section which includes pages about λs and method references.
 
reply
    Bookmark Topic Watch Topic
  • New Topic