泛型 java语法,Java泛型语法如何帮助避免类型转换?

Below is the code,

import java.util.List;

import java.util.ArrayList;

public class Dummy {

public static void main(String[] args) {

List lst = new ArrayList();

lst.add("a string");

lst.add("another string");

String s = lst.get(0);

} //end main

}

when the constructor new ArrayList(); is invoked, array of type Object is created.

6u9Rx.png

..

lst holds Object[0] array.

l3h1G.png

So, If array of type Object gets created by constructor, How does javac does not see type casting issue in this statement String s = lst.get(0);, despite generic syntax is used while invoking constructor?

解决方案

Here is some non-generic code:

public class MyList {

List myData = new ArrayList();

public void add(Object ob) {

myData.add(ob);

}

public Object getAtIndex(int ix) {

return myData.get(ix);

}

}

This code will allow you to store an object of any type into your MyList instances even if the contract of MyList specifies that the objects must all be of the same type. Moreover if you use a MyList instance to store String instances, you must manually cast them to String when you retrieve them.

String myString = (String) myList.get(1);

The above MyList class is not type safe. It is most definitely possible that the above assignment statement will fail with a ClassCastException if an object other than a String instance was stored into your MyList instance (which can happen at run-time without any complaint).

Here is a generified MyList class:

public class MyList {

List myData = new ArrayList<>();

public void add(T ob) {

myData.add(ob);

}

public T getAtIndex(int ix) {

return myData.get(ix);

}

}

Now, the compiler guarantees that only T instances can be added and retrieved from MyList instances. Because the compiler guarantees that T instances will always be returned, you can use syntax like this without any manual casting:

String myString = myList.get(1);

The generified MyList class is now type-safe. The compiler won't allow you to store anything but T instances into your MyList instances, which guarantees that no ClassCastExceptions will occur at run-time. If you examine the byte-code, you'll find that the compiler has placed a cast automatically.

Generics in Java are a compile-time only phenomenon. In the byte-code, all references to T in the MyList class above are replaced by Object. This process is referred to as "type erasure". It is important to remember that type safety in Java is provided by the compiler only. If your program compiles without any errors AND without any warnings, then your program, generics and all, is type safe. The compiled program, however, has retained almost no information about the generic parameters.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值