Java Raw Type and generics interaction
Asked Answered
H

2

7

If I have a Stack class

class Stack<E> {}

now if I do:

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

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

3) Stack s = new Stack()

can anyone explain me what these interactions (generic<->raw) 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?

Hypo answered 30/7, 2009 at 11:33 Comment(2)
I don't get it - do you want to have a stack where you can push other things then Integers (e.g. Integers and Doubles) or do you want to use the stack only with one type (Integer) and let the compiler help you?Pastelki
No I want a stack with one type and want to know what happen if I do that assignments.Hypo
S
7

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.

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; pushing non-Integer values will cause a type error.

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.

Synergist answered 30/7, 2009 at 21:7 Comment(0)
P
6

All of them are unsafe because Java generics, by virtue of type erasure, are just syntactic sugar. For example, this is entirely valid Java code:

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. That's it. They do nothing more than that (except generate compiler and IDE warnings).

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.

Purine answered 30/7, 2009 at 11:36 Comment(2)
isn't OP's case #2 one where the compiler is smart enough to infer the generic type arguments for s?Gastrotomy
Yes but in point 1 the compiler advises me if I try to put (with a push operation) an object other than an Integer. So may be generics aren't only a 'syntactic sugar' but point 1 changes also the compiler's behavior (the semantic). So again I don't understand where is the unsafe... Should, please, give me a more analytic explaination?Hypo

© 2022 - 2024 — McMap. All rights reserved.