Java Virtal Machine.net

[ News ] rss

February 01, 2011

Mark Wielaard: New GPG key. Finally created a new GPG key using gnupg. The old one was a DSA/1024 bits one and 8 years old. The new one is a RSA/2048 bits one. I will use the new one in the future to sign any release tarballs I might create. pub 2048R/57816A6A 2011-01-29 Key f...

More »

February 01, 2011

Andrew Hughes: [SECURITY] IcedTea6 1.7.8, 1.8.5, 1.9.5 Released!. We are pleased to announce a new set of security releases, IcedTea6 1.7.8, IcedTea6 1.8.5 and IcedTea6 1.9.5. This update contains the following security updates: The IcedTea project provides a harness to build the source code from OpenJDK6 u...

More »

April/2024
SunMonTueWedThuFriSat
 123456
78910111213
141516 17181920
21222324252627
282930    

[ Archives News
for 'Java Technology' ]

home > news > java technology > two problems with generics in java

Two problems with generics in Java

Since generics were out and I started using them, there were always a few cases in which I couldn't make them do what I wanted. I always thought it was my problem, and that I didn't understand what was going on... Turns out: it's not. There are at least two things that are implemented in a way that break what I thought were very safe expectations. Inference Before Generics, you would write something like: List list = Collections.EMPTY_LIST; Adding generics you get a warning: List list1 = Collections.EMPTY_LIST; GenericsGotchas.java:[19,40] [unchecked] unchecked conversion found : java.util.List required: java.util.List The problem is that we have a raw type on the right, and a generic type on the other. We could suppress the warning every single time, but there is a better option: using a generic method and let the compiler infer and match the types List list2 = Collections.emptyList(); This compiles without a problem. But this does not: List list3 = new ArrayList(Collections.emptyList()); GenericsGotchas.java:[21,29] cannot find symbol symbol : constructor ArrayList(java.util.List) location: class java.util.ArrayList anotherExpression(expression); is always valid. Unfortunately, it's not true in this corner case. So, when I am refactoring my code, or decorating an expression, I have now one more thing to remember and to pay attention to. Casting generics type Castin...


Date: April, 09 2010
Url: http://www.java.net/blog/carcassi/archive/2010/04/09/two-problems-generics-java-0


Others News

©2002-2019Java-Virtual-Machine.net free icon fonts | best website design software | best web design software | bootstrap navbar | image slider