lundi sept. 29, 2008

SDPY - JavaEE 5, Java 5, ...

•   POJOs and ABAP (SAP now Java EE 5-certified) (2006)
I was asking who would be last ship a Java EE 5-certified product. So, IBM or JBoss?

•   Tiger is here! (2004)
Java SE 5 shipped four (!) years ago. Are you there yet?

vendredi août 06, 2004

Generics: First steps, first questions



when generics lead to generics... So, moving along in my summer homework, I finally took some time to start playing (not just reading) with Generics.

Option 0 (Default):
        List<String> list = new LinkedList<String>();    // default (no issue here)
        list.add("ABC");
        for (Object element : list) {
            System.out.println (element.toString());
        }

Option 1:
        // this code compiles (with lint warnings) and runs without type-safety
        List list = new LinkedList<String>();   // valid but what's the point?
        list.add("ABC");
        list.add(new Float(10));
        for (Object element : list) {
            System.out.println (element.toString());
        }

Option 2:
        // code does not compile due to 3rd line
        // if you take it out, behaves like Option 0.
        List<String> list = new LinkedList();   // valid syntax
        list.add("ABC");
        list.add(new Float(10));   // compile error - cannot find symbol : method add(java.lang.Float)
        for (Object element : list) {
            System.out.println (element.toString());
        }

Option 3:
        // does not compile, types need to be the same. Inheritance doesn't work here.
        // but Abstract types are allowed (replacing Float with Number fixes the error).

        List<Number> list = new LinkedList<Float>();   // "Incompatible types" compile error
        list.add("ABC");    // compile error - cannot find symbol : method add(java.lang.String)
        list.add(new Float(10));
        for (Object element : list) {
            System.out.println (element.toString());
        }

Option 1 is strange, I'm not sure what it means and why it's allowed. Compatibility with previous versions probably.

Sounds like Option 2 is nice because it does the same as Default, only it doesn't require you to type in twice the generic type. The only advantage to Option 0 may be that it's more readable.

Option 3 simply shows abstract types are allowed.


Update: On a similar note, check out this post
About

This blog has moved

Alexis Moussine-Pouchkine's Weblog

GlassFish - Stay Connected

Search

Archives
« avril 2014
lun.mar.mer.jeu.ven.sam.dim.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today
Blogroll

No bookmarks in folder

News

No bookmarks in folder