Re: Erased data type
Randall R Schulz <[email protected]>
| Newsgroups | gmane.comp.windows.devel.java.advanced |
|---|---|
| Message-ID | <[email protected]> |
On Thursday 30 August 2007 08:10, Mann, Ivan H wrote: > I have an app with a gui that can develop a lot of data. There is a > save button, which writes the data out using serialization. > > Then, there is a load button, which reads the data back in, using > this line: > > destinations = (ArrayList<ScheduleDestination>) > is.readObject() ; > > Where destinations is defined elsewhere as > > ArrayList<ScheduleDestination> destinations ; > > On the line that reads the object back in, there is a compiler flag: > > Type Safety: The cast from Object to > ArrayList<ScheduleDestination> is actually checking against the > erased type ArrayList. > > What does this flag mean? I have googled fairly creatively and can't > find anything which tells me what it means or how to make it go away. Java generics are implemented via "type erasure," which means that the types that parameterize a generic type in the source code have no representation whatsoever in the compiled bytecodes (they're "erased"). All the type inference and verification that is to be done must be done statically by the Java compiler. So it's telling you that all you're really casting to is ArrayList, not ArrayList<anythingInParticular> and thus it cannot be guaranteed that somewhere down the line a ClassCastException might be produced when you extract an item from that list. See Angelika Langer's detailed Generics "FAQ" (I use the scare quotes 'cause it's really much more than a FAQâit's a comprehensive tutorial): <http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html> > Ivan Mann Randall Schulz =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com