views:

56

answers:

2
List listOne = new LinkedList<Shxx>();      
List<Shxx> listTwo = new LinkedList<Shxx>();
List listThree = new LinkedList();
List<Shxx> listFour = new LinkedList();
+7  A: 
List listOne = new LinkedList<Shxx>();

Throws away the type information, might as well not use generics at all.

List<Shxx> listTwo = new LinkedList<Shxx>();

Correct full use of generics, provides type safety.

List listThree = new LinkedList();

No use of generics (i.e. pre Java 5 code), no type safety.

List<Shxx> listFour = new LinkedList();

Will cause a compiler warning, but otherwise OK because the list can only be used through the typesafe reference and is initially empty. Shouldn't be done anyway because if you ignore compiler warnings concerning the use of raw types, you could also be ignoring others that are not as benign as this one. The best way to get the maximum type safety out of generics is to eliminate all related compiler warnings.

Michael Borgwardt
great but what do u meaning by "might"? at first point, did u mean the inserted element will be handle like Object type ?
MineIsMine
Please: it's 'you', not 'u'.
seanizer
sorry forgive me for this mistake
MineIsMine
@MineIsMine: yes, the effect is exactly the same as with listThree, there is no usable type information.
Michael Borgwardt
+1  A: 

listOne and listThree will only be usable in code as lists of objects (additionnally, your IDE may show warnings for both, as instanciation of listOne implies an unchecked use of a checked collection, while instanciation of listThree is a raw use of what should be a checked collection).

For both theses lists, you'll be able to add any kind of object (String as an archetypal example).

listTwo is declared as a classical List of Shxx.

For this list, compiler will only allow you to add items of the Shxx type.

Declaration of listFour compiles, and makes list usable (like listTwo) as a List of Shxx (but I wouldn't recommand such writing).

Finally, although these lists won't be usable in the same fashion in your declared code, you have to know that they'll all be equivalent at Runtime (but you theorically won't be able to overcome those generics limitations before a long time).

Riduidel
@Declaration of listFour compiles just fine for me.
Michael Borgwardt
@Michael_Borgwardt mmh, yes, you're, right, I'll change my answer accordingly
Riduidel
Thank you >>>>> it's working with me.
MineIsMine