Effective Java 42 Use varargs judiciously
Implementation theory
The varargs facility works by first creating an array whose size is the number of arguments passed at the call site, then putting the argument values into the array, and finally passing the array to the method.
// Simple use of varargs
static int sum(int... args) {
int sum = 0;
for (int arg : args)
sum += arg;
return sum;
}
// The right way to use varargs to pass one or more arguments
static int min(int firstArg, int... remainingArgs) {
int min = firstArg;
for (int arg : remainingArgs)
if (arg < min)
min = arg;
return min;
}
Usage
- Designed for printf(added in release 1.5).
- Core reflection facility, was retrofitted to take advantage of varargs in that release.
Note
- Don't retrofit every method that has a final array parameter; use varargs only when a call really operates on a variable-length sequence of values.
// The wrong way to print an array
public static void main(String[] args) {
int[] digits = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 4 };
System.out.println(Arrays.asList(digits));
/* This will print [[I@2cdb03a1], since The Arrays.asList method, now "enhanced" to use varargs, gathers up the object reference to the int array digits into a one-element array of arrays and dutifully wraps it into a List<int[]>instance. */
}
// The right way to print an array
System.out.println(Arrays.toString(digits));
// This will print [ 3, 1, 4, 1, 5, 9, 2, 6, 5, 4]
2. Every invocation of a varargs method causes an array allocation and initialization it will introduce the performance issue. There is a pattern to solve this. 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) { }
3. EnumSet is good example for this which provides performance-competitive replacements for bit fields(Item 32).
Summary
Varargs methods are a convenient way to define methods that require a variable number of arguments, but they should not be overused. They can produce confusing results if used inappropriately.
Effective Java 42 Use varargs judiciously的更多相关文章
- Effective Java 11 Override clone judiciously
Principles If you override the clone method in a nonfinal class, you should return an object obtaine ...
- Effective Java 41 Use overloading judiciously
The choice of which overloading to invoke is made at compile time. // Broken! - What does this progr ...
- Effective Java 74 Implement Serializable judiciously
Disadvantage of Serializable A major cost of implementing Serializable is that it decreases the flex ...
- Effective Java Index
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
- 《Effective Java》读书笔记 - 7.方法
Chapter 7 Methods Item 38: Check parameters for validity 直接举例吧: /** * ...其他的被我省略了 * @throws Arithmet ...
- Effective Java 第三版——42.lambda表达式优于匿名类
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Effective Java 第三版——32.合理地结合泛型和可变参数
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Effective Java 目录
<Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
随机推荐
- EPANET头文件解读系列8——FUNCS.H
/*************************************************************************** ...
- Hibernate核心类用法-使用Transaction管理事务
一个典型的事务应该使用下面的形式 在创建完Session对象后即使用beginTransaction()启动事务 从此开始直到commit()之间的代码 都会处于同一个事务中 这两个函数之间所有的数据 ...
- 【第一课】神奇的Context
初学Android的困惑 初学Android跳转页面的时候,往往教程里是这么写的: Intent intent = new Intent(); //MyActivity就是当前的Activity,It ...
- [Solution] Microsoft Windows 服务(3) 使用Quartz.net定时任务
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,Quartz.net 就是Quartz的移植版本.Quartz可以用来创建简单或为运行十个,百个,甚至是 ...
- PHP图像处理类库及演示分享
简单写了一个PHP的图像处理类库,虽然功能比较少,但是目前也没用到太高级的,以后用到了再填吧,或者哪位给点建议加上什么功能,或者有什么需求可以跟我说,我有时间加上,如果哪位对这个类库进行了扩展的话,还 ...
- X3DOM新增剪裁平面节点ClipPlane支持
裁剪平面由方程Ax+By+Cz+D=0确定.所有满足[A B C D]M-1[Xe Ye Ze We]T>0的人眼坐标[Xe Ye Ze We]的点都位于该平面定义的半空间中,而该半空间以外的所 ...
- 用Perl编写Apache模块
前言 Apache被许多大流量网站所嫌弃,但很多企业级的场景则更为适用. Apache httpd 从 2.0 之后,已经不仅仅局限于一个 http 的服务器,更是一个完善而强大.灵活而健壮且容易扩展 ...
- php5.6-Apache2.4-mysql5.6环境配置(win7_64位)
----------------------------------------------------- ★软件工具:(下载时注意下载相应版本,不同版本安装细节可能会有差异!!) 1>http ...
- Python参数组合
参数定义的顺序必须是:①必选参数.②默认参数.③可选参数.④命名关键字参数.⑤关键字参数 #a,b为必选参数:c为默认参数:args为可变参数:kw为关键字参数 def f1(a,b,c=0,*arg ...
- j2ee log4j集中式日志解决方案logpool v0.3
V0.3相对于v0.2的更新如下: