aspose file tools*
The moose likes Java in General and the fly likes Question on Collection class of Java 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question on Collection class of Java 5" Watch "Question on Collection class of Java 5" New topic
Author

Question on Collection class of Java 5

michael yeung
Greenhorn

Joined: Oct 20, 2004
Posts: 16
In Java 5, we can initialize the object of collection class like the following:

List<String> list = new ArrayList<String>();

Does the coding <String> mean we should add only String object into the List?

Thanks!!!


MCP (70-210), OCP9i DBA, SCJP 1.4 (SAI)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yup. It does two nice things for you. It prevents you from adding anything but a String with compile time checking, and it lets you eliminate casting when you take your Strings back out. Sweeeet.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

It prevents you from adding anything but a String with compile time checking, and it lets you eliminate casting when you take your Strings back out

And if you declare the object as Object, it just simply goes back to the original style that you could add anything to a list.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Originally posted by michael yeung:
In Java 5, we can initialize the object of collection class like the following:

List<String> list = new ArrayList<String>();

Does the coding <String> mean we should add only String object into the List?

Thanks!!!


yes,

you can add type class and subclass.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
FYI, this question fits under the broader scope of Generics, which is a new feature in Tiger. If you are interested in learning more about how these work, just google for it or use the Search tool here at the Ranch. This topic has been discussed a lot lately with the advent of Java 5.


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Collection class of Java 5
 
Similar Threads
GENERICS
Key Advantages of Java 5
Generics doubt
Generics Question.
mock test for generics & collections