Till now I manage to find all answers I need but this one confusing me. Let's say we have example code:
public class Animal {
private String species;
private boolean canHop;
private boolean canSwim;
public Animal(String speciesName, boolean hopper, boolean swimmer) {
species = speciesName;
canHop = hopper;
canSwim = swimmer;
}
public boolean canHop() { return canHop; }
public boolean canSwim() { return canSwim; }
public String toString() { return species; }
}
public interface CheckAnimal {
public boolean test(Animal a);
}
public class FindSameAnimals {
private static void print(Animal animal, CheckAnimal trait) {
if(trait.test(animal)){
System.out.println(animal);
}
public static void main(String[] args) {
print(new Animal("fish", false, true), a -> a.canHop());
}
}
OCA Study Guide (Exam 1Z0-808) book says that these two lines are equivalent:
a -> a.canHop()
(Animal a) -> { return a.canHop(); }
Does this mean that, behind the scenes, Java adds keyword return to code in the first case?
If answer is YES then how next code compile (imagine everything else is in proper place):
static int counter = 0;
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(() -> counter++));
if we know that signatures for execute and Runnable's run are:
void execute(Runnable command)
void run()
If answer is NO then how Java know when it need to return something and when not to? Maybe in
a -> a.canHop()
case we wanted to ignore boolean return type of method.