当前位置: 首页 > 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版本配套关系
  • 用群晖做网站/厦门百度广告
  • 一站式网站建设平台/百度关键词点击价格查询
  • 网络网页制作/seo日常优化内容是什么
  • 问卷调查网站哪个好/产品互联网营销推广
  • 武夷山网站建设/成年学校培训班
  • 二级域名免费注册网站/软文营销案例文章