File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes java 1.5 generic and autocasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java 1.5 generic and autocasting" Watch "java 1.5 generic and autocasting" New topic

java 1.5 generic and autocasting

Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 107

Generics - adds compile-time type safety to the collections framework and eliminates the need to cast when reading elements from collections.
Enhanced for loop - eliminates the need for explicit iterators when interating over collections.
Autoboxing/unboxing - automatically converts primitives (such as int) to wrapper classes (such as Integer) when inserting them into collections, and converts wrapper class instances to primitives when reading from collections.

I think the following code should work, but the compiler complains " Incompatbable type for Object and int or Integer" , what is wrong with me for generic and autocasting?


[ January 17, 2008: Message edited by: Steve Jiang ]
Rob Spoor

Joined: Oct 27, 2005
Posts: 19759

You declared the HashMap without generics, so it only knows that it returns Object.

If you change that into HashMap<String, Integer>, or even better Map<String, Integer> it should work.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: java 1.5 generic and autocasting