java requirenonnull_具有供应商参数性能的Java 8 requireNonNull方法

在Java 8中添加了Supplier的方法Objects.requireNonNull,但是我不确定声明的性能改进是什么:

While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly.

带String的方法如果不为null则忽略参数:

public static T requireNonNull(T obj, String message) {

if (obj == null)

throw new NullPointerException(message);

return obj;

}

In JDK 7, java.util.Objects included several methods to check for null, including one that took a message to return if a null was found. With lambdas in JDK 8, another variant to include is a requireNonNull method which takes a string supplier instead of a string. That why the cost of creating the string message can be avoided for the non-null case. Note that the lambda capture can have a nonzero cost though.

有注释表示对性能没有影响:

The non-zero capture cost does worry me. I am concerned that it will frequently erase any advantage of using a Supplier.

09-04-2013

我发现了其他问题,但没有提到(为什么)发送String参数会降低性能

它专用于lambda表达式/流用法吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值