match在java中,为什么Streams.allMatch(在Java8中)试图尝试评估所有表达式,即使可以在中间确定值时也是如此?...

Consider this snippet -

String a = "hello" , b = null, c = "guru";

boolean value = Stream

.of(a, b, b.substring(2),c)

.allMatch(x -> x != null);

System.out.println(value);

This results in NPE. It seems to be doing b.substring(2) and since b is null, NPE is thrown.

Why is this condition evaluated? The second expression b is null and hence evaluates to false.

So, allMatch will be false regardless of the truth values of the subsequent operations. In that case, why is it trying to evaluate b.substring(2)?

This article and this article claim that it may not evaluate all the expressions if not necessary for determining the result. That clearly does not seem to be the case here.

Prior to Java 7 -

if(b != null && b.substring(2) != null)

this would not throw NPE because b!= null is false and it would return false there itself.

So, can we say that Streams.allMatch is not an exact equivalent of the above Java code snippet?

解决方案

Streams.allMatch is not an exact equivalent of the above Java code

snippet?

Of course it is not! Streams are very new way of thinking the processing of data.

Anyway your problem is (not related to this) just that for any function call, arguments are evaluated, thus the call to of necessitate the evaluation of b.subString(2) which obviously throw a NPE.

Related info from the Java Language Specification (emphasize is mine):

15.7.4 Argument Lists are Evaluated Left-to-Right

In a method or constructor invocation or class instance creation

expression, argument expressions may appear within the parentheses,

separated by commas. Each argument expression appears to be fully

evaluated before any part of any argument expression to its right.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值