Search code examples
javalazy-evaluation

Lazy Evaluation - Java


Can somebody make an detailed explanation (and i mean really detailed, im dumb) of lazy evaluation in java and maybe also explain what is meant with eager evaluation?

Why is this bringing a runtime-error when b = 0?

if (a/b < 2 && b>0)//execute something 

And why does this not give one?

if (b>0 && a/b < 2)//execute something 

Both should be cases of lazy evaluation, but i just dont get why one is working and the other one isn't.


Solution

  • a/b < 2 gets a Division by zero error wich will be executed in the first example.

    In the second example b>0 will be evaluated first , is false, so the second part will not be evaluated, because the first part is false and the complete result can't be true