Lazy Evaluation – Java 懒加载 – Java

huangapple go评论45阅读模式
英文:

Lazy Evaluation - Java

问题

以下是翻译的内容:

请问有人能详细解释一下Java中的惰性求值(我指的是真正详细的解释,因为我不太懂),并可能解释一下什么是急切求值吗?

当b = 0时,为什么会引发运行时错误?

if (a/b < 2 && b>0)//执行某些操作

而这段代码为什么不会产生错误?

if (b>0 && a/b < 2)//执行某些操作

这两者都应该是惰性求值的情况,但我不明白为什么一个能正常工作,另一个却不行。

英文:

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 &lt; 2 &amp;&amp; b&gt;0)//execute something 

And why does this not give one?

if (b&gt;0 &amp;&amp; a/b &lt; 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.

答案1

得分: 2

`a/b < 2` 
在第一个示例中会得到除零错误。

在第二个示例中,`b>0` 会先被评估,但是为假,所以第二部分不会被评估,因为第一部分是假的,整个结果不能为真。
英文:

a/b &lt; 2
gets a Division by zero error wich will be executed in the first example.

In the second example b&gt;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

huangapple
  • 本文由 发表于 2023年2月19日 23:56:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定