Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Type Safety

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand and I can't see the difference between



Can somebody help me?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The first list generates a warning about type safety : The expression of type array list needs unchecked conversion to conform to List<Integer>

The second list does not generate any warning.

The reason the first list is generating a warning is because we are trying to use a raw type. ( ArrayList()) .

What the compiler is saying is that it would have to do an unchecked conversion which could have been avoided if we had used the generic type.
The compiler trusts you that the list contained only Integer's prior to the assignment.

But, since generics use type erasure, at least in this case, it doesnt affect us in any way. That is ,ArrayList<Integer> uses the raw type ArrayList.

<digression>
Btw, no type information is stored in run time. While I agree that generics give us the ability to do more type checks during compile time,
it doesnt allow us to do certain things. A typical Java developer needs to be aware of these things...

Java is no longer "simple" ..Combining non generic code with generics will make the code hard to understand...as neal grafter mentions in his blog, backward compatibility is hard...:-(
http://gafter.blogspot.com/2004/09/puzzling-through-erasure-answer.html

</digression>

[ January 09, 2008: Message edited by: Raman Gopalan ]
[ January 09, 2008: Message edited by: Raman Gopalan ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic