简体   繁体   English

Java Raw Type和泛型交互

[英]Java Raw Type and generics interaction

If I have a Stack class 如果我有一个Stack类

class Stack<E> {}

now if I do: 现在,如果我这样做:

1) Stack<Integer> s = new Stack() 1) Stack<Integer> s = new Stack()

2) Stack s = new Stack<Integer>() 2) Stack s = new Stack<Integer>()

3) Stack s = new Stack() 3) Stack s = new Stack()

can anyone explain me what these interactions (generic<->raw) causes? 任何人都能解释一下这些互动(通用< - > raw)会导致什么?

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. 主要是我的疑点是第1点。事实上,如果我这样做,那么它是不安全的,因为该堆栈可以存储除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? 是的,但是如果我有一个push方法并且尝试存储除Integer以外的值,那么编译器会阻止我...所以当我有不安全的操作时?

All three are perfectly legal, since there is no actual runtime difference between a Stack and a Stack<Integer> , but all three will result in compiler warnings. 这三个都是完全合法的,因为StackStack<Integer>之间没有实际的运行时差异,但这三个都会导致编译器警告。

Stack<Integer> 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; 但是,在这种情况下这样做是完全安全的:推入Integer值不会导致任何错误; pushing non- Integer values will cause a type error. 推送非Integer数值将导致类型错误。

Stack s = new Stack<Integer>()

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 . Stack引用类型Stack时,您也可能会收到“原始类型”警告。

All of them are unsafe because Java generics, by virtue of type erasure , are just syntactic sugar. 所有这些都是不安全的,因为Java泛型,由于类型擦除 ,只是语法糖。 For example, this is entirely valid Java code: 例如,这是完全有效的Java代码:

Stack<Integer> s = new Stack<Integer>();
Stack<Double> s2 = (Stack<Double>)s;
s2.push(3.3d);

The point of Java generics is that stop you having to be explicit about casting Objects. Java泛型的要点是阻止你必须明确地构建对象。 That's it. 而已。 They do nothing more than that (except generate compiler and IDE warnings). 他们只做那件事(除了生成编译器和IDE警告)。

They are still useful and can make your code much more readable and less error-prone but ultimately, at a byte code level, it's important to understand they're not doing anything. 它们仍然很有用,可以使您的代码更易读,更不容易出错,但最终,在字节码级别,了解它们没有做任何事情是很重要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM