Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

This Generics work (At least in my compiler)

 
Jose Campana
Ranch Hand
Posts: 339
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahoy there ! brotherhood of Java (otherwise known as Java Ranch)

The following question is about Generics.......

I have noticed that it doesn't really matter if you don't write the Generic type of a Collection on the right side, I mean on the actual object as long as you type it on the reference variable side.

Like this:



I think this still behaves as a Generic heterogeneous Collection, but... the reverse isn't true, right? (correct me If I'm wrong please...)

something like this...



What are the implications for both cases ?

Could someone please tell me...

Good Luck,

Jose

[ April 16, 2008: Message edited by: Jose Campana ]
[ April 16, 2008: Message edited by: Jose Campana ]
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Campana:
Ahoy there ! brotherhood of Java (otherwise know as Java Ranch)


I think this still behaves as a Generic heterogeneous Collection, but... the reverse isn't true, right? (correct me If I'm wrong please...)

something like this...

List mice = new ArrayList<Mouse>();

What are the implications for both cases ?

Could someone please tell me...

Good Luck,

Jose


hey even i tried

i found that a string can be added to the listInt1 and its not giving any compile time error.
But


this restrict me using string.
m also trying to find why ?
 
Jose Campana
Ranch Hand
Posts: 339
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Harshit, it's weird isn't it ?



Thanks for your reply!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics are more or less an illusion, conjured by the compiler. An ArrayList object does not know that it's being pointed at by a variable of type List<Integer>, any more than it know it's being pointed at by a variable of type List instead of ArrayList. Type checks in generic collections are all done by the compiler at compile time; there are no exceptions or anything that happen if the "wrong" type is stored in a collection at runtime.

So this is why the generic type of the object, as allocated, doesn't really matter, but the type of the variable pointing to it does.

Make sense?
 
Jose Campana
Ranch Hand
Posts: 339
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Friedman-Hill,

I suppose it does make sense, Could I assume this topic belongs to something relative to type erasure?

I never knew it could get so complex. However I thought it was worth asking this question since it was never explained in any book out there.

Sorry for the delayed response.

Have a nice day!
Sincerely,
Jose
 
abhishek pendkay
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes your question does have to do with type erasure

Jose Campana
However I thought it was worth asking this question since it was never explained in any book out there.


type erasure is explained in many books including K&B and Complete Reference etc so its surprising you didnt come across it , ofcourse you will only find it in books on Java version 5 and above...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic