| Author |
This Generics work (At least in my compiler)
|
Jose Campana
Ranch Hand
Joined: May 28, 2007
Posts: 339
|
|
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
Joined: Apr 15, 2008
Posts: 131
|
|
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 ?
|
<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
|
 |
Jose Campana
Ranch Hand
Joined: May 28, 2007
Posts: 339
|
|
Hey Harshit, it's weird isn't it ? Thanks for your reply!
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
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?
|
[Jess in Action][AskingGoodQuestions]
|
 |
Jose Campana
Ranch Hand
Joined: May 28, 2007
Posts: 339
|
|
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
Joined: Jan 01, 2007
Posts: 184
|
|
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...
|
The significant problems we face cannot be solved by the same level of thinking which created them – Einstein
SCJP 1.5, SCWCD, SCBCD in the making
|
 |
 |
|
|
subject: This Generics work (At least in my compiler)
|
|
|