1Z0-809 · Question #77
Given: class CheckClass { public static int checkValue (String s1, String s2) { return s1.length() ?s2.length(); } } and the code fragment: String[] strArray = new String [] {"Tiger", "Rat", "Cat"…
The correct answer is A. Arrays.sort(strArray, CheckClass :: checkValue). Option A works because checkValue is a static method with the signature (String, String) → int, which exactly matches the Comparator<String> functional interface. The syntax ClassName::staticMethodName is the correct method reference form for static methods, so…
Question
Options
- AArrays.sort(strArray, CheckClass :: checkValue);
- BArrays.sort(strArray, (CheckClass :: new) :: checkValue);
- CArrays.sort(strArray, (CheckClass :: new).checkValue);
- DArrays.sort(strArray, CheckClass :: new :: checkValue);
How the community answered
(39 responses)- A74% (29)
- B15% (6)
- C3% (1)
- D8% (3)
Explanation
Option A works because checkValue is a static method with the signature (String, String) → int, which exactly matches the Comparator<String> functional interface. The syntax ClassName::staticMethodName is the correct method reference form for static methods, so Arrays.sort(strArray, CheckClass::checkValue) sorts the array by ascending string length (Rat=3, Cat=3, Lion=4, Tiger=5), producing the expected output.
Options B and D are wrong because Java does not allow chaining :: operators - (CheckClass::new)::checkValue and CheckClass::new::checkValue are both syntactically illegal; a constructor reference is a functional interface value, not an object you can further dereference with ::. Option C fails for the same reason: (CheckClass::new) produces a functional interface reference, not an actual instance, so you cannot call .checkValue on it with dot notation.
Memory tip: For static method references, think Class::method - no new, no chaining. If you see ::new, that's a constructor reference (e.g., ArrayList::new), and it cannot be combined with another :: or . to reach a method.
Community Discussion
No community discussion yet for this question.