Created
June 26, 2016 22:51
-
-
Save thieux/18aa7e96ce9687de0a26eea9275cbb07 to your computer and use it in GitHub Desktop.
Java8 method reference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Java8MethodRef { | |
interface Equality1 { | |
boolean eq(Object a); | |
} | |
interface Equality2 { | |
boolean eq(Object a, Object b); | |
} | |
interface Equality3 { | |
boolean eq(Object a, Object b, Object c); | |
} | |
public void compatibleTypes() { | |
Equality2 a = Object::equals; | |
} | |
public void incompatibleTypes() { | |
// invalid method reference | |
// non-static method equals(java.lang.Object) cannot be referenced from a static context | |
Equality1 e1 = Object::equals; | |
// incompatible types: invalid method reference | |
// method equals in class java.lang.Object cannot be applied to given types | |
// required: java.lang.Object | |
// found: java.lang.Object,java.lang.Object,java.lang.Object | |
// reason: actual and formal argument lists differ in length | |
Equality3 e = Object::equals; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment