9

For instance why can I write the line

Character[] c = Arrays.sort(list.toArray(new Character[list.size()]))

But in documentation when I read about method referencing, they tell me to use :: instead? Doesn't it do the same as the . operator?

I dont know if the above code compiles, as I'm typing this on my mobile. Consider it a loose example.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
  • 3
    ummmmmm... `::` is a syntax error in Java. What documentation did you read? – tckmn Dec 15 '13 at 20:47
  • 3
    Are you reading a tutorial about Java 8 ? – Alexis C. Dec 15 '13 at 20:48
  • 1
    Could you provide the link where you are read this? It's not Java, it's C++. – Bosko Mijin Dec 15 '13 at 20:48
  • 6
    `::` is `C++` syntax, not `Java`... – nhgrif Dec 15 '13 at 20:48
  • 5
    @nhgrif Not in Java 8 =) – Alexis C. Dec 15 '13 at 20:49
  • @ZouZou, java8 have something like this? – msangel Dec 15 '13 at 20:50
  • 5
    @msangel http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html – Alexis C. Dec 15 '13 at 20:50
  • 1
    *"Beta Draft 2013-10-15 This section was updated to reflect features and conventions of the upcoming Java SE 8 release. You can download the current JDK 8 snapshot from java.net."* This stuff isn't ready yet, if you're new to Java you shouldn't go there yet. – Esko Dec 15 '13 at 20:51
  • @msangel Yes, they're called Method Referenes in Java 8. – Jeroen Vannevel Dec 15 '13 at 20:51
  • [This answer](http://stackoverflow.com/a/20001866/1217087) explains how the `::` operator is used in Java 8. – Adam S Dec 15 '13 at 20:54
  • @Michanne a broader discussion on the :: operator from java 8, you can find here: http://stackoverflow.com/questions/20001427/double-colon-operator-in-java-8/20001548#20001548 – Olimpiu POP Dec 15 '13 at 20:59
  • Thanks guys. I reached the point where I was learning about detailed features of classes and methods and stumbled on to method referencing while learning about Lambdas. They should have given it its own section –  Dec 15 '13 at 21:14

1 Answers1

12

The double colon operator is a new operator provided in Java8. It is syntactic sugar that tells the compiler to generate a lambda based on context which will call the method. This makes some lambda expression things a bit easier. Prior to Java8 this operator doesn't exist, and no, its not the same as the dot(.) operator. For example:

Math.max(4, 6) // Calls Math.max with the arguments 4 and 6
Math::max // A reference to the max method in the java.lang.Math class

For a bit of extra reading (Although this stuff is all in Beta and has not been officially released) try http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html

Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
  • That is exactly where I saw it, I was learning about Lambdas and it confused the hell out of me –  Dec 15 '13 at 21:10
  • It is really to bring Java in line with C++ etc on the whole method pointers and anonymous functions things. – Sinkingpoint Dec 15 '13 at 21:10
  • but what is Math.max, without arguments? couldnt that have served as the desired reference? – peter Oct 03 '22 at 19:04