java泛型详解

网友投稿 200 2023-06-01


java泛型详解

首先请看如下代码:

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 h = new Hashtable();

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.class或是List.class,而只有List.class。

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

public class generictype

{

public static void main(String str[]){

test1 t = new test1();

test1 tt = new test1();

System.out.println(t.a);

System.out.println(tt.a);

}

}

class test1{

static int a = 1;

}

//结果

1

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

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

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

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

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

public class generictype {public static void main(String str[]) {

test3 t =new test3();

t.getT("11111");

}

}

interface test2{

public T getT(T t);

}

class test3 implements test2{

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 t = new test();

t.put(1, "str1");

t.put(2, "str2");

System.out.println(t.get(1));

System.out.println(t.get(2));

}

}

class test {

public Hashtable h = new Hashtable();

public void put(T t, V v) {

h.put(t, v);

}

public V get(T t) {

return h.get(t);

}

}

//执行结果

str1

str2

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

public class generictype {

public 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"));

}

}

//执行结果

a

a

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

public class generictype {

public static void main(String str[]) {

TestException t =new TestException();

try {

t.excute(2);

} catch (IOException e)JlxVK {

e.printStackTrace();

}

}

}

//extends说明该泛型参数继承于Exception

interface TestExceptionInterface

{

public void excute(int i) throws T;

}

class TestException implements TestExceptionInterface{

@Override

public void excute(int i) throws IOException {

if(i<10){

throw new IOException();

}

}

}

//意义:1.针对不同的可能出现的异常类型,定义自己的实现类。

2.定义多个实现类的时候,不用一个一个手动throws异常,提高了代码重用率


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 泛型总结(三):通配符的使用
下一篇:Java实现读取文章中重复出现的中文字符串
相关文章

 发表评论

暂时没有评论,来抢沙发吧~