I was studying through the Practice
Test book by Scott Selikoff and Jeanne Boyarsky. I was going through the chapter on Generics and Collections where I found a question like :
Why can't String::charAt be used as a method refernce ?
The answer given in the book was..
Method references can pass either the instance or the parameter from the lambda,but not both.
The answer was further explained in the solutions as...
With method references, only one item can be supplied at runtime. Here, we need either a String instance with no parameter in the method or a static method with a single parameter. The charAt() method is an instance method with a single parameter so does not meet this requirement.
I did not understand the answer given so I decided to try it out and I wrote this code...
The code runs fine and output is as expected but what I understand is the book says that charAt() method cannot be used via method references... I am confused.
Where am I going wrong ? or is their a mistake in the book ?