java泛型详解

时间:2021-05-19

首先请看如下代码:

public class generictype { public static void main(String str[]) { Hashtable h =new Hashtable(); h.put(1, "String类型"); int a = (String) h.get(1); System.out.println(a); }}//执行结果String类型//如果我们将上述由红色标出的String改为int执行后结果如下(更改后编译没有错误):Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at genetictype.generictype.main(generic1.java:10)

以上就是强制类型转换可能带来的典型错误,然而这个错误在编译期间无法知道,以至于在运行期间jvm检查后抛出类型转换异常。

再看下述代码:

public class generictype { public static void main(String str[]) { Hashtable<Integer, String> h = new Hashtable<Integer, String>(); h.put(1, "String类型"); String a= h.get(1); System.out.println(a); }}//执行结果string类型//需要提出的是1.上述由红色标出的String如果改为int,在编译的时候会报错 2.在h.get(1)前面不需要再进行强制类型转换。

综上看来泛型的作用为:

1.就是是在编译的时候检查类型的安全(解决java中强制类型转换可能导致的错误),交给了编译器巨大的使命。

2.提高代码的重用率

类型擦除:

类型擦除就是说编译器编译.java文件时,将类的泛型参数去掉,那么jvm加载字节码文件的时候对泛型不可见,这个过程就称为类型擦除。

与类型擦除有关的现象:

(1) 泛型类没有Class的类类型。比如并不存在List<String>.class或是List<Integer>.class,而只有List.class。

(2) 静态变量是被泛型类的所有实例所共享的。

public class generictype { public static void main(String str[]){ test1<String> t = new test1<String>(); test1<Date> tt = new test1<Date>(); System.out.println(t.a); System.out.println(tt.a); }}class test1<T>{ static int a = 1;}//结果1

(3) 泛型的类型参数错误不能通过异常处理,因为异常处理是jvm实现的,而jvm加载的字节码文件已经擦除了泛型特征,这也间接的说明了泛型的意义:在编译期间发现参数类型错误。

类型擦除的基本过程也比较简单:

1.将类型参数用顶级父类替换,这类一般是Object,如果指定了类型参数的上界的话,则使用这个上界。

2.去掉出现的类型声明,即去掉<>的内容。

例如:T get()方法声明就变成了Object get();List<String>就变成了List。接下来就可能需要生成一些桥接方法(bridge method)。这是由于擦除了类型之后的类可能缺少某些必须的方法。比如考虑下面的代码:

public class generictype {public static void main(String str[]) { test3 t =new test3(); t.getT("11111"); }}interface test2<T>{ public T getT(T t);}class test3 implements test2<String>{ public String getT(String t){ return t; }}//类型擦除后的代码public class generictype { public static void main(String str[]) { test3 t = new test3(); t.getT("11111"); } interface test2 { public Object getT(Object t); } class test3 implements test2 { public String getT(String T){ return T } public Object getT(Object t) { return this.getT((String) t); }//如果没有这段代码,在类型擦除后test3没有重写接口test2的抽象方法,明显错误,因此编译器的巨大作用就是在这里帮忙生成了该方法,同时编译器也依靠该功能完成检错任务。}

泛型的分类:泛型类,泛型接口,泛型方法,泛型异常

泛型类

public class generictype { public static void main(String str[]) { test<Integer, String> t = new test<Integer, String>(); t.put(1, "str1"); t.put(2, "str2"); System.out.println(t.get(1)); System.out.println(t.get(2)); }}class test<T, V> { public Hashtable<T, V> h = new Hashtable<T, V>(); public void put(T t, V v) { h.put(t, v); } public V get(T t) { return h.get(t); }}//执行结果str1str2

多态方法(泛型方法):在函数名前定义泛型参数,可以在传入参数列表,返回值类型,方法体里面引用

public class generictype {public <T> String getString(T obj){ return obj.toString();} public static void main(String str[]) { generictype g =new generictype ();//不需要类的泛型 System.out.println(g.getString(1)); System.out.println(g.getString('a')); System.out.println(g.getString("a")); }}//执行结果aa

泛型异常(兼具泛型接口)

public class generictype { public static void main(String str[]) { TestException t =new TestException(); try { t.excute(2); } catch (IOException e) { e.printStackTrace(); } }}//extends说明该泛型参数继承于Exceptioninterface TestExceptionInterface<T extends Exception>{ public void excute(int i) throws T;}class TestException implements TestExceptionInterface<IOException>{ @Override public void excute(int i) throws IOException { if(i<10){ throw new IOException(); } }}//意义:1.针对不同的可能出现的异常类型,定义自己的实现类。 2.定义多个实现类的时候,不用一个一个手动throws异常,提高了代码重用率

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章