- 浏览: 596122 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
月光杯:
问题解决了吗?
Exceptions in HDFS -
iostreamin:
神,好厉害,这是我找到的唯一可以ac的Java代码,厉害。
[leetcode] word ladder II -
standalone:
One answer I agree with:引用Whene ...
How many string objects are created? -
DiaoCow:
不错!,一开始对这些确实容易犯迷糊
erlang中的冒号 分号 和 句号 -
standalone:
Exception in thread "main& ...
one java interview question
参见
http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html
To use Java generics effectively, you must consider the following restrictions:
Cannot Instantiate Generic Types with Primitive Types
这个是说你不能用primitive type来做类型参数,就是说下面这个使用泛型的例子是不对的:
Cannot Create Instances of Type Parameters
这个是不能直接new T()来创建类型参数的实例。下面的例子里,在run time,有关T或者E的类型信息都找不到了,所以你必须用反射的技术来做到:
这里反射的技术就是传一个类作为参数。
Cannot Declare Static Fields Whose Types are Type Parameters
这个容易引起混乱的,看下面这个例子:
如果类的静态变量允许是类型参数:
那么os的实际类型是什么呢?乱套了肯定。所以Java禁止这样做。
Cannot Use Casts or instanceof with Parameterized Types
因为java在编译是做了type erasure,所以无法区分ArrayList<Integer>和ArrayList<String>,所以Java不允许如下code编译通过:
Cannot Create Arrays of Parameterized Types
也就是说下面这行code会编译错误。
List<Integer>[] arrayOfLists = new List<Integer>[2]; // compile error
为什么要禁止这个呢?先看不同类型的object插入到array里会怎么样:
换成generic list呢,假设下面第一行code能够编译通过,那么第三行code在运行时就无法抛出ArrayStoreException了。所以Java要禁掉这样写法。
Cannot Create, Catch, or Throw Objects of Parameterized Types
一个generic的class不能继承Throwable(间接或直接都不行), 不能catch类型参数的实例(但可以throw)。例如:
这些都会编译错误。
Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type
这个是说因为type erasure的原因,会导致像
两个print方法完全一样,所以也被禁止。
以上是java文档上列出来的,还有一种情况:
第三行会编译错误“Cannot instantiate the type ArrayList<? extends Foo>", 原因是如果这行ok,那么编译器就不知道是否add一个SubFoo是安全的,如果foos被赋值成ArrayList<AltFoo> 呢?记住,compiler要在编译期解决所有类型检查的,所以这些能引起混乱的它肯定要禁掉了。参见stackoverflow上面的回答。
多说一点的是更详细的关于genercis的使用,参见
http://tutorials.jenkov.com/java-generics/wildcards.html,
关于? extends A的解释是List<? extends A>是只知道是一个list,可以装A或者A的子类,所以你可以读取里面的内容并cast成A, 但是不能insert。
http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html
To use Java generics effectively, you must consider the following restrictions:
- Cannot Instantiate Generic Types with Primitive Types
- Cannot Create Instances of Type Parameters
- Cannot Declare Static Fields Whose Types are Type Parameters
- Cannot Use Casts or instanceof With Parameterized Types
- Cannot Create Arrays of Parameterized Types
- Cannot Create, Catch, or Throw Objects of Parameterized Types
- Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type
Cannot Instantiate Generic Types with Primitive Types
这个是说你不能用primitive type来做类型参数,就是说下面这个使用泛型的例子是不对的:
class Pair<K, V> { private K key; private V value; public Pair(K key, V value) { this.key = key; this.value = value; } // ... } Pair<int, char> p = new Pair<>(8, 'a'); // compile-time error
Cannot Create Instances of Type Parameters
这个是不能直接new T()来创建类型参数的实例。下面的例子里,在run time,有关T或者E的类型信息都找不到了,所以你必须用反射的技术来做到:
T instantiateElementType(List<T> arg) { return new T(); //causes a compilation error } public static <E> void append(List<E> list, Class<E> cls) throws Exception { E elem = cls.newInstance(); // OK list.add(elem); }
这里反射的技术就是传一个类作为参数。
Cannot Declare Static Fields Whose Types are Type Parameters
这个容易引起混乱的,看下面这个例子:
如果类的静态变量允许是类型参数:
public class MobileDevice<T> { private static T os; // ... } MobileDevice<Smartphone> phone = new MobileDevice<>(); MobileDevice<Pager> pager = new MobileDevice<>(); MobileDevice<TabletPC> pc = new MobileDevice<>();
那么os的实际类型是什么呢?乱套了肯定。所以Java禁止这样做。
Cannot Use Casts or instanceof with Parameterized Types
因为java在编译是做了type erasure,所以无法区分ArrayList<Integer>和ArrayList<String>,所以Java不允许如下code编译通过:
public static <E> void rtti(List<E> list) { if (list instanceof ArrayList<Integer>) { // compile-time error // ... } }
Cannot Create Arrays of Parameterized Types
也就是说下面这行code会编译错误。
List<Integer>[] arrayOfLists = new List<Integer>[2]; // compile error
为什么要禁止这个呢?先看不同类型的object插入到array里会怎么样:
Object[] strings = new String[2]; strings[0] = "hi"; // OK strings[1] = 100; // An ArrayStoreException is thrown.
换成generic list呢,假设下面第一行code能够编译通过,那么第三行code在运行时就无法抛出ArrayStoreException了。所以Java要禁掉这样写法。
Object[] stringLists = new List<String>[]; // compiler error, but pretend it's allowed stringLists[0] = new ArrayList<String>(); // OK stringLists[1] = new ArrayList<Integer>(); // An ArrayStoreException should be thrown, // but the runtime can't detect it.
Cannot Create, Catch, or Throw Objects of Parameterized Types
一个generic的class不能继承Throwable(间接或直接都不行), 不能catch类型参数的实例(但可以throw)。例如:
// Extends Throwable indirectly class MathException<T> extends Exception { /* ... */ } // compile-time error // Extends Throwable directly class QueueFullException<T> extends Throwable { /* ... */ // compile-time error public static <T extends Exception, J> void execute(List<J> jobs) { try { for (J job : jobs) // ... } catch (T e) { // compile-time error // ... } }
这些都会编译错误。
Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type
这个是说因为type erasure的原因,会导致像
public class Example { public void print(Set<String> strSet) { } public void print(Set<Integer> intSet) { } }
两个print方法完全一样,所以也被禁止。
以上是java文档上列出来的,还有一种情况:
public List<? extends Foo> getFoos() { List<? extends Foo> foos = new ArrayList<? extends Foo>(); foos.add(new SubFoo()); return foos; }
第三行会编译错误“Cannot instantiate the type ArrayList<? extends Foo>", 原因是如果这行ok,那么编译器就不知道是否add一个SubFoo是安全的,如果foos被赋值成ArrayList<AltFoo> 呢?记住,compiler要在编译期解决所有类型检查的,所以这些能引起混乱的它肯定要禁掉了。参见stackoverflow上面的回答。
多说一点的是更详细的关于genercis的使用,参见
http://tutorials.jenkov.com/java-generics/wildcards.html,
关于? extends A的解释是List<? extends A>是只知道是一个list,可以装A或者A的子类,所以你可以读取里面的内容并cast成A, 但是不能insert。
发表评论
文章已被作者锁定,不允许评论。
-
ssl 与 java 实例
2014-01-27 10:10 753http://www.iteye.com/topic/1125 ... -
Java NIO
2014-01-10 21:28 707看了这个java nio的教程,明白了什么是Selector. ... -
再谈Java的wait(), sleep(), notify()和notifyAll()
2013-07-25 10:59 1874一段时间不用java,这些概念就全混淆了,有必要彻底澄清一下, ... -
Why singleton is anti-pattern?
2013-07-03 10:12 875OO Test Other reasons? -
How to generate the serialVersionUID when you implement Serializable interface,j
2013-07-01 10:52 921http://docs.oracle.com/javase/6 ... -
Java Override的两个问题
2013-06-01 11:40 9281: 如果子类中的方法的参数是父类的方法的子类型,那么算不算o ... -
Java常用类API统计
2013-06-01 11:35 0String charAt(int) compareTo( ... -
How many string objects are created?
2013-06-01 10:18 1317This is a very common java inte ... -
使用Java的DelayQueue容易碰到的一个坑
2013-05-27 17:32 6673今天不忙,学习一下java.util.concurrent.D ... -
[leetcode] Balanced Binary Tree
2013-04-28 14:08 1564Check if a binary tree is balan ... -
[leetcode] find median of two sorted arrays
2013-04-26 10:55 1439http://leetcode.com/onlinejudge ... -
[leetcode] word ladder
2013-04-25 15:05 2259Q: Given two words (start and ... -
[leetcode] word ladder II
2013-04-15 07:35 11648http://leetcode.com/onlinejudge ... -
[leetcode] Count and Say
2013-04-12 14:05 2243http://leetcode.com/onlinejudge ... -
Date/Time处理函数总结 [To Do]
2013-04-12 10:46 644几种我所用到的用来处理日期,时间的函数总结。 Perl 1 ... -
[leetcode] Palindrome Partition
2013-04-12 10:25 1309http://leetcode.com/onlinejudge ... -
[leetcode] Palindrome Partitioning II
2013-04-11 16:45 1496http://leetcode.com/onlinejudge ... -
Profiling your Java code using Spring
2013-03-05 15:02 665Quite good article!!! http://w ... -
Overriding Method Using Parameter That is a Subclass?
2012-12-27 22:14 878参见 http://www.coderanch.com/t/3 ... -
Java的Generics和c++的Template到底有什么不同?
2012-12-27 16:21 3186先了解Java的Generics: 根据Java的文档,Jav ...
相关推荐
Java Generics and Collections
Java Generics and Collections
找了好久都只有Java泛型与集合这本书的chm版本,现在终于找到了pdf版本,分享一下
Java Generics Cheatsheet, your convenient way to check and learn!
NULL 博文链接:https://wuaner.iteye.com/blog/907980
Java Generics and Collections.chm Java语言基础部分 必学知识。
[Java泛型和集合].(Java.Generics.and.Collections).Maurice.Naftalin&Philip.Wadler.文字版.
英文资源,_Java_Generics_and_Collections
Java Generics and Collections.2006.pdf
Java Generics FAQ
Java.Generics.and.Collections.Oct.2006.pdf 完美清晰正版
Java 的 Generics 简介。入门资料!
Java generics and collections
Generics_in_the_Java_Programming_Language的中文译文
2.8 对通配符的限制 第三章(集合类) 3.1 可比较的 3.2 集合的最大值 3.3 水果相关示例 3.4 比较 3.5 枚举类型 3.6 多重界限 3.7 桥接器 3.8 协变覆盖 第四章(声明) 4.1 构造函数 4.2 静态成员 4.3 嵌套类 4.4 擦除的...
在有关Java核心的系列文章中,我们将继续学习2个新内容,即Generics和Collection,它们是Java中非常流行的对象。 泛型格式化参数化数据类型,以便我们可以将类,接口或方法用于许多不同的数据类型。 集合只是具有...
[Java泛型和集合].(Java.Generics.and.Collections).Maurice.Naftalin&Philip.Wadler.文字版