wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What can i do and don't with this collection declaration ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What can i do and don Watch "What can i do and don New topic
Author

What can i do and don't with this collection declaration ?

Faber Siagian
Ranch Hand

Joined: Jul 08, 2008
Posts: 52
What can i do and can't be done with the following collection declarations ?

1. List<MyClass> list = new ArrayList();
2. List list2 = new ArrayList<String>();


Thanks.


Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (88 %)
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
list is type save, so you can can add directly only elements of type MyClass and the compiler warns you if there is a possiblility, that list gets an elements of a different type; in fact line 1 itself will produce a compiler warning).

list2 isn't type save and can contain elements of every type.


SCJP 5 (98%) - SCBCD 5 (98%)
Faber Siagian
Ranch Hand

Joined: Jul 08, 2008
Posts: 52
If i add elements into list2 like shown below :



what happen to object referred by list2 ? i have added three different objects into it, but list2 actually is an String type-safe ArrayList.
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
list2 actually is an String type-safe ArrayList.
No, the reference type counts here and that's "List".

Note: Generics are a compile time issue. After compilation, list2 refers to an usual Arraylist without any type restrictions (this is called "type erasure"). By the way, it's interesting to watch the compiled code using a decompiler when generics come into play. In your samples it would look like

List list = new ArrayList();
List list2 = new ArrayList();
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
Originally posted by Ralph Jaus:
list is type save, so you can can add directly only elements of type MyClass and the compiler warns you if there is a possiblility, that list gets an elements of a different type; in fact line 1 itself will produce a compiler warning).

list2 isn't type save and can contain elements of every type.


Adding a little to what Ralph said ...
That was adding part ... while retrieving data back ...
you will get object of type Object from list2 as it is not type safe. you will have to type cast it to MyClass or whatever you stored in that order

Whereas for list, you will get a MyClass object you need not explicitly cast it


SCJP 6 83%
Working on SCWCD/OCPJWCD
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What can i do and don't with this collection declaration ?
 
Similar Threads
how to add an array to an ArrayList
initializing ArrayList
Question about generics
Question for Wildcards in Collection
K&B generics doubt