About Question enthuware.ocajp.i.v8.2.1473 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
FrodoBaggins
Posts: 14
Joined: Tue Jun 02, 2015 8:32 am
Contact:

About Question enthuware.ocajp.i.v8.2.1473 :

Post by FrodoBaggins »

Hi there.

Just to point out that to get this to compile I had to import three different imports.

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1473 :

Post by admin »

Unless the question is about import statements, you may assume appropriate imports. Many questions in the exam contain just a code fragment. You have to assume that the rest of the code is in place and valid.
If you like our products and services, please help us by posting your review here.

islam.amin099
Posts: 4
Joined: Mon Jul 20, 2015 1:47 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1473 :

Post by islam.amin099 »

Hi there,
checkList(new ArrayList(), (ArrayList al) -> al.isEmpty());
If the Predicate requires a List and ArrayList implements List why this doesn't compile?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1473 :

Post by admin »

You have to think of a lambda expression as a complete class to understand why you cannot use ArrayList instead of a List here.
The lambda expression (ArrayList al) -> al.isEmpty() is actually same as the following class:

Code: Select all

class MyPredicate implements Predicate<List>{ //Here MyPredicate has to implement Predicate<List> because the method checkList has Predicate<List>
   public boolean test(ArrayList al){ //Here the method parameter type is ArrayList because in the lambda expression, you have (ArrayList al).
     return al.isEmpty();
   }
}
You are saying that MyPredicate is a Predicate<List>, which means that its method should be able to accept any kind of List. But in the method test(ArrayList al), you are saying that it will only accept an ArrayList. Do you see that both are contradictory statements? That is why it is not acceptable.

Remember that when you write a lambda expression, you not just passing an object. You are actually declaring a method. Therefore, that method must be compatible with the interface that you are trying to implement with that lambda expression.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

islam.amin099
Posts: 4
Joined: Mon Jul 20, 2015 1:47 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1473 :

Post by islam.amin099 »

I got it now. Thank you for the clarification.

articulatesnail
Posts: 1
Joined: Thu Mar 18, 2021 12:38 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1473 :

Post by articulatesnail »

Code: Select all

checkList(new ArrayList(), al -> al.isEmpty());
Then for this statement (correct answer), is the al passed in "auto-widened" to List?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1473 :

Post by admin »

No, the question of al being auto-widened does not arise because al is not a preexisting variable. al is a new variable for this scope and its type is List.
Think of the lambda expression al->al.isEmpty() like this:

Code: Select all

class SomeClass implements Predicate<List>{
  public boolean test(List al){  <-- The type of al is interpreted by the compiler to be List
   al.isEmpty();
  }
}
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 90 guests