java raw type什么意思,Java Raw Type和泛型交互

If I have a Stack class

class Stack {}

now if I do:

1) Stack s = new Stack()

2) Stack s = new Stack()

3) Stack s = new Stack()

can anyone explain me what these interactions (genericraw) causes?

Mainly my doubt is on point 1. In fact if I do that the assignment it's unsafe because that stack can store types other then Integer. Yes but if I have a push method and try to store

a value othern than an Integer the compiler stop me...so when I'd have that unsafe operation?

解决方案

All three are perfectly legal, since there is no actual runtime difference between a Stack and a Stack, but all three will result in compiler warnings.

Stack s = new Stack()

This will result in an "unchecked conversion" warning, because it's not safe in general to convert a raw type to a parameterized type. However, it's perfectly safe to do so in this case: pushing Integer values will not cause any errors; pushing non-Integer values will cause a type error.

Stack s = new Stack()

This is a legal conversion from a parameterized type to a raw type. You will be able to push value of any type. However, any such operation will result in an "unchecked call" warning.

Stack s = new Stack()

Again, this is legal, with no implicit conversion. You will be able to push value of any type. However, any such operation will result in an "unchecked call" warning.

You may also get a "raw type" warning any time you refer to the type Stack.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值