Collections.max函数申明中的泛型
来源:互联网 发布:推筒子软件 编辑:程序博客网 时间:2024/05/21 07:13
Sometimes a dummy bound does the trick.Occasionally, one must pay attention to the fact that a generification might change the signature of some methods in the byte code. Changing the signature will break existing code that cannot be recompiled and relies on the binary compatibility of the old and new version of the .class file.
Example (before generification, taken from package java.util ):
class Collections {The max method finds the largest element in a collection and obviously the declared return type of the method should match the element type of the collection passed to the method. A conceivable generification could look like this.
public static Object max( Collection coll) {...}
...
}
Example (after a naive generification):
class Collections {While this generification preserves the semantics of the method, it changes the signature of the max method. It is now a method with return type Comparable , instead of Object .
public static <T extends Comparable<? super T>>
T max(Collection <? extends T> coll) {...}
...
}
Example (after type erasure):
class Collections {This will break existing code that relies on the binary compatibility of the .class files. In order to preserve the signature and thus the binary compatibility, an otherwise superfluous bound can be used.
public static Comparable max( Collection coll) {...}
...
}
Example (after binary compatible generification, as available in package java.util ):
class Collections {The leftmost bound of the type parameter is now type Object instead of type Comparable , so that the type parameter T is replaced by Object during type erasure.
public stati c <T extends Object & Comparable<? super T>>
T max(Collection <? extends T> coll) {...}
...
}
Example (after type erasure):
class Collections {
public static Object max( Collection coll) {...}
...
}
Afterthought:
Perhaps you wonder why the hack decribed in this FAQ entry is needed. Indeed, had the Collections.max method been defined as returning a Comparable in the first place, no further measures, such as adding Object as a type parameter bound, had been required to preserve binary compatibility. Basically, the declared return type Object is a mistake in the design of this method.
If you carefully study the specification of the Collections.max method's functionality then you realize that all elements of the collection are required to implement the Comparable interface. Consequently, the returned object is Comparable , too. There is no reason why the method should return an Object reference.
The only explanation one can think of is that in pre-generic Java there was no way of ensuring by compile-time type checks that the Collection contains only Comparable objects. However, this was ensured via runtime type checks, namely an explicit downcast in the implementation of the method. Hence this is not really an excuse for the bug.
Note, that the runtime time type check in the pre-generic version of the Collections.max method still exists in the generic version. The former explicit cast is now an implicit one generated by the compiler. In the generic version, this cast can never fail (unless there are unchecked warnings), because the type parameter bound Comparable ensures at compile-time that the elements in the Collection are Comparable .
- Collections.max函数申明中的泛型
- 函数申明中的 __P 的作用
- matlab中的max函数
- matlab中的 MAX函数
- matlab中的max函数
- 集合(Collections-sort)-(Collections-max)-(Collections-binarySearch)
- MySQL中的MAX函数总结
- 细说MATLAB中的max函数
- static函数的申明
- 函数的申明
- static函数的申明
- vbs中的最大值类似max、min函数
- linux内核中的min、max函数
- [转]linux内核中的min、max函数
- Linux内核中的Min和Max函数
- linux内核中的min、max函数
- linux内核中的min、max函数
- linux内核中的min、max函数
- iOS开发之xib技巧介绍
- 缅怀!2014年离我们而去的名人们
- Apache Spark源码走读之18 -- 使用Intellij idea调试Spark源码)(zhuan)
- 新浪微博数据挖掘食谱之六: 元素篇 (提取微博元素)
- swift语言IOS8开发战记4.custom tableViewCell
- Collections.max函数申明中的泛型
- 反汇编调试死循环
- Neerc2011 Dictionary Size
- 充实的2014,技术仍然浮在表面
- apt-get常用命令
- jquery部分知识
- 织梦安装详解以及常见问题
- App被App Store Rejected 的各种原因翻译
- 北京500辆纯电动出租车今天上路