当前位置: 首页 > news >正文

代码与细节(一)

在用到 Java17的新特性 Unmodifiable Lists 时不知道你是否和我有同样的惊讶

为什么弄了这么多重载方法?

先说结论:为了性能。

其实一细想,都能想明白:varargs(可变参数) 的背后是数组的内存分配和初始化,相比正常的传参涉及更多的内存开销。

所以任何便利都是有代价的。下面列出一段大神兼偶像 Joshua Bloch 《Effective Java - Third Edition》Item53:Use varargs judiciously 的原文作为佐证:

Exercise care when using varargs in performance-critical situations. Every invocation of a varargs method causes an array allocation and initialization. If you have determined empirically that you can't afford this cost but you need the flexibility of varargs, there is a pattern that lets you have your cake and eat it too.
Suppose you've determined that 95 percent of the calls to a method have three or fewer parameters. Then declare five overloadings of the method, one each with zero through three ordinary parameters, and a single varargs method for use when the number of arguments exceeds three:

public void foo() { }
public void foo(int a1) {}
public void foo(int a1, int a2) { }
public void foo(int a1, int a2, int a3) {}
public void foo(int a1, int a2, int a3, int... rest) { }

相关文章:

  • 广东广西大量工地建筑支模
  • 京东API商品详情页,商品列表数据,商品评论数据采集
  • 2023NOIP A层联测14-选举
  • BootStrap-前端框架
  • JVM第七讲:JVM 基础 - Java 内存模型详解
  • gitlab版本库安装
  • 微信公众号粉丝迁移步骤?
  • 如何使用 MiniGPT-v2
  • Base64编码
  • 制药企业液体制剂生产设备管理利器:中央设备状态监控系统CMS
  • 儿童带磁性写字板入驻亚马逊CPC认证怎么做?CPC认证是什么样子的解析
  • Android AMS——进程优先级更新(十八)
  • 网络库OKHTTP(2)面试题
  • uml知识点学习
  • css 特别样式记录
  • 面试题:线程池中线程抛了异常,该如何处理?
  • 关于我对 jeecg-boot 的项目理解、使用心得和改进建议
  • 项目管理之5W2H项目定义法
  • linux 下的java gate服务断掉的原因及解决思路
  • JDK版本和Gradle版本配套关系
  • A股2024年年报披露收官,四分之三公司盈利
  • 阿根廷发生5.6级地震,震源深度30公里
  • 铁路12306回应“五一前大量放票”传闻:个别方向和区段出现新增票额,均即时进入系统重新发售
  • 讲武谈兵|朝鲜“崔贤”号驱逐舰下水,朝版“宙斯盾”战力如何?
  • 七部门联合发布《终端设备直连卫星服务管理规定》
  • 五大光伏龙头一季度亏损超80亿元,行业冬天难言结束