Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top