tag:blogger.com,1999:blog-6017814538727105125.post4443495594178029746..comments2022-12-04T20:21:00.009-08:00Comments on Functr: Java Oddities (Part I)Unknownnoreply@blogger.comBlogger5125tag:blogger.com,1999:blog-6017814538727105125.post-17826266847621194922013-05-08T04:45:58.864-07:002013-05-08T04:45:58.864-07:00semi mid level Java developer here, great post for...semi mid level Java developer here, great post for me .thinktankhttps://www.blogger.com/profile/07701607514796006407noreply@blogger.comtag:blogger.com,1999:blog-6017814538727105125.post-76961702761101407582012-09-28T02:11:20.022-07:002012-09-28T02:11:20.022-07:00No, generics /are/ safe (in the absence of an unch...No, generics /are/ safe (in the absence of an unchecked warning). The cast doesn't change that; the second line of your example doesn't compile. Anonymoushttps://www.blogger.com/profile/14423949360886851191noreply@blogger.comtag:blogger.com,1999:blog-6017814538727105125.post-83614046961720756602012-08-31T13:25:23.005-07:002012-08-31T13:25:23.005-07:00Generics are actually invariant in Java, i.e given...Generics are actually invariant in Java, i.e given a parametric class C<E> and given a type S that is a subtype of T, there is no relation between C<S> and C<T>. This means that the following line is a compile error in Java:<br /><br />ArrayList<Object> myList = new ArrayList<String>(); // compile error<br /><br />Type casts are a different story. They are unsafe by their nature as you basically take over the type system.<br />Raoul-Gabriel Urmahttps://www.blogger.com/profile/13568644152784832812noreply@blogger.comtag:blogger.com,1999:blog-6017814538727105125.post-33446190495161823652012-08-30T14:40:49.701-07:002012-08-30T14:40:49.701-07:00Nice post! I'm a DZone.com curator and I'...Nice post! I'm a DZone.com curator and I'd love to feature this on Javalobby if you're interested. Shoot me an email at mpron[at]dzone[dot]comDZONEMVBhttps://www.blogger.com/profile/02587941708923838103noreply@blogger.comtag:blogger.com,1999:blog-6017814538727105125.post-50862060228541599472012-08-30T13:54:15.702-07:002012-08-30T13:54:15.702-07:00Even generics are not safe, since they are covaria...Even generics are not safe, since they are covariant as well, and can be freely typecast away (think C-style void* casting) due to erasure:<br /><br />ArrayList<String> myList = new ArrayList<String>();<br />ArrayList<Object> castList = (ArrayList<Object>)myList; // the road to hell is paved with good intentions...<br />ArrayList<Integer> brokenList = (ArrayList<Integer>)castList; // oh dear, here we go...<br />brokenList.add(42); // KABOOM!<br /><br />(btw, is there any way to type angle brackets in here without "Your html tag is not allowed" errors or resorting to character entities?)Edhttps://www.blogger.com/profile/12186766341193266503noreply@blogger.com