Java Magazine, Mar/Apr 2018
ORACLE COM JAVAMAGAZINE MARCH APRIL 2018 102 fix this Option C suggests that the lambda cannot be created unless the target interface Something is annotated with @ FunctionalInterface While it is a good idea to annotate an interface thats created specifically for the purpose of supporting lambdas with this annotation its only a means of getting a more helpful error report from the compiler Specifically if an interface carries this annotation the annotation will report an error if the interface contains more than a single abstract method On the other hand if the annotation is not present the errors will show up whenever any attempt is made to create a lambda expression using the interface Its generally more helpful to have an error reported as close to its cause as possible rather than being reported when a consequential problem arises However because the existing code does not fail to compile and because it prints Bonjour rather than Hello option C is incorrect The syntax suggested in option D will not compile This is an attempt to resolve ambiguous access to default methods in interfaces However this form cannot be used in this situation so option D is incorrect Option E employs a syntax that is normally used to access shadowed elements of an enclosing class although in this case the class of this is already TryThis The syntax compiles and does result in the output of Bonjour However option E is incorrect because the original code does not fail to compile Answer 4 The correct answer is option E This is one of those questions that tend to annoy people It requires you to spot a subtle programming error However this is an error that the compiler cannot spot and that does not cause any visible problems at runtime other than a wrong answer and you would be hard pressed to look up a solution in a reference document Unless you just know the relevant detail you run the risk of falling into this trap So lets discuss the various options First the code compiles and executes without any errors being reported The call to parallel is merely a distraction in this question it is completely correct but entirely irrelevant It doesnt matter where in the sequence of the stream operations this call is placed the call has the same efect regardless Further the call afects the entire stream not merely
You must have JavaScript enabled to view digital editions.