Win a copy of Murach's MySQL this week in the JDBC and Relational Databases 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why doesn't Java5 automatically type Collections on declaration (or throw error)...

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I do..

List list = new ArrayList<FooBar>():

This above is not a type safe list unless I had declared it as:

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

I'm just curious if there is any reason why they didn't decide to throw a compiler error here or else automatically make List typed to FooBar objects as well?

On a declaration of assignment like the above I can't imagine why you'd want something else other than a List of <FooBar> objects since that's how you are assigning it. You could be easily misled into thinking you are making a type safe list of FooBar objects, when that isn't the case.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One reason I can think of is all the existing Java code that was written prior to version 5. If you extended some of those classes and tried to recompile, if the compiler automatically threw an error if you didn't type a Collection, you would have to go through all the old code and change every use of a Collection.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the frustration at the redundancy of typing:

List<Type> aList = new ArrayList<Type>();

But Java is a verbose language and sometimes you can't get around that. One work-around is to create a static creation method, and allow the compiler to infer the type of the generic parameter.


[ June 25, 2007: Message edited by: Garrett Rowe ]
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Garrett Rowe:
I understand the frustration at the redundancy of typing:

List<Type> aList = new ArrayList<Type>();



It's not just a matter of a typing redundancy but it could give someone the false impression they are actually creating a type-safe list. Sure you could say "Well be careful, and know what you're doing!" (Also, yes, most IDEs give warnings about using non typed Collections.)

I guess it is mostly for some sort of backward compatibility issue that it 'is the way it is.'
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic