• Post Reply Bookmark Topic Watch Topic
  • New Topic

generics  RSS feed

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any reason to avoid primitve types in generics
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean? You can't use primitives as type arguments, if that's what you mean. For instance List<int> is illegal, you should use List<Integer>.
 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:What do you mean? You can't use primitives as type arguments, if that's what you mean. For instance List<int> is illegal, you should use List<Integer>.


i know that type parameter only accepts reference types (ex:Ineger,Double) but i would like to know that why the developers of java provides this restriction(not to use primitve type);

 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the original implementations for Lists, etc, accepted Objects; that excludes primitives. This was of course in the days before boxing.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case of primitives compiler already checks the type and no unexpected type mismatch at run time , which cause cast exception.Where as in case of objects object type can be decided at run time as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!