Runtime type checking. Compile time type checking. Autoboxing

Document Sample
Runtime type checking. Compile time type checking. Autoboxing Powered By Docstoc
					             Runtime type checking.                                    Compile time type checking.
    LinkedList list = new LinkedList();                         LinkedList<Integer> list = new LinkedList<Integer>();
    list.add(new Integer(1));                                   list.add(new Integer(1));
                                                                Integer num = list.get(0);
    Integer num = (Integer) list.get(0);




                                                               // Creating your own generic classes.
                                                               public class Pair<E1,E2> {
            Autoboxing - unboxing.                                E1 first;
                                                                  E2 second;
 LinkedList<Integer> list = new LinkedList<Integer>();            public Pair() {
 list.add(1);                                                     }
 int num = list.get(0);                                           public Pair(E1 first, E2 second) {
                                                                    this.first = first;
                                                                    this.second = second;
                                                                  }
                                                                  public String toString() {
                                                                    return "(" + first + ", " + second + ")";
                                                                  }
                                                               }




class PairTest {                                               public class PairC<E1 extends Comparable<E1>,
  public static void main(String[] args) {                                        E2 extends Comparable<E2>>
    Pair<Integer,Double> pair1 = new Pair<Integer,Double>();   {
    pair1.first = 1;                                             E1 first;
    pair1.second = 2.2;                                          E2 second;
    System.out.println(pair1);                                   public PairC() {
    Pair<Integer,String> pair2 = readInput();                    }
    System.out.println(pair2);                                   public PairC(E1 first, E2 second) {
  }                                                                this.first = first;
  static Pair<Integer,String> readInput() {                        this.second = second;
    int number = 1; // could read from somewhere                 }
    String str = "one"; //likewise read from somewhere           public String toString() {
    return new Pair<Integer,String>(number, str);                  return "(" + first + ", " + second + ")";
  }                                                              }
}




                                                                                                                        1
    public int compareTo(PairC<E1,E2> obj) {                                    import java.util.Scanner;
      int compareFirst =
                                                                                class PairTest {
                      first.compareTo(obj.first);
                                                                                  public static void main(String[] args) {
      if (compareFirst == 0)                                                       Scanner input = new Scanner(System.in);
        return second.compareTo(obj.second);                                       while(true) {
      else                                                                           PairC<Integer,String> pair1 = readInput(input);
        return compareFirst;                                                         PairC<Integer,String> pair2 = readInput(input);
                                                                                     System.out.println("comparing " + pair1 + " and "
    }
                                                                                      + pair2 + " you get " + pair1.compareTo(pair2));
}                                                                                  }
                                                                                 }
                                                                                  static PairC<Integer,String> readInput(Scanner input) {
                                                                                   System.out.println("Enter an int and a string.");
                                                                                   int number = input.nextInt();
                                                                                   String str = input.next();
                                                                                   return new PairC<Integer,String>(number, str);
                                                                                 }
                                                                                }




import java.util.ArrayList;                                                     import java.util.ArrayList;
class ArrayTest {                                                               class ArrayTest {
   public static void main(String[] args) {                                        public static void main(String[] args) {
     ArrayList strList = new ArrayList();                                            ArrayList<String> strList = new ArrayList<String>();
     strList.add("hello");                                                           strList.add("hello");
     String x = (String)strList.get(0);                                              String x = strList.get(0);
   }                                                                               }
}                                                                               }




public static void main(java.lang.String[]);                                    sundance>diff genericArrayTest.txt noGenericArrayTest.txt
 Code:                                                                          1c1
 0: new #2; //class java/util/ArrayList
                                                                                < Compiled from "ArrayTest.java"
 3: dup
 4: invokespecial #3; //Method java/util/ArrayList."<init>":()V                 ---
 7: astore_1                                                                    > Compiled from "ArrayTest2.java"
 8: aload_1
 9: ldc #4; //String hello
 11: invokevirtual #5; //Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
 14: pop
 15: aload_1
 16: iconst_0
 17: invokevirtual #6; //Method java/util/ArrayList.get:(I)Ljava/lang/Object;
 20: checkcast       #7; //class java/lang/String
 23: astore_2
 24: return




                                                                                                                                            2
List<String> ls = new List<String>();        List<String> ls = new List<String>();
List<Object> lo = ls;                        List<Object> lo = ls;

                                             lo.add(new Object());
                                             String s = ls.get(0);




void printCollection(Collection c) {         void printCollection(Collection<Object> c) {
  Iterator i = c.iterator();                   for (Object e : c) {
  for (k = 0; k < c.size(); k++) {               System.out.println(e);
    System.out.println(i.next());              }
                                             }
  }
}




void printCollection(Collection<?> c) {
  for (Object e : c) {
    System.out.println(e);                                           My Questions
  }
}                                                • What good is Collection<?> c = new
                                                   ArrayList<String>(); if I can’t add anything to it?
                                                 • What can you do with ? that you can’t do without
Collection<?> c = new ArrayList<String>();
                                                   it?
c.add(new Object()); // compile time error       • When do generics generate different code?
c.add(new String()); // compile time error       • When does the subtype principle apply to generics
                                                   (if at all)?




                                                                                                         3
import java.util.*;                                             ArrayList<Integer> intList = new ArrayList<Integer>();
class Wild2 {
                                                                ArrayList<Double> dblList = new ArrayList<Double>();
  public static void main(String[] args) {
    ArrayList<Integer> intList= new ArrayList<Integer>();
    intList.add(new Integer(1));                                ArrayList<? extends Number> c = intList;
    ArrayList<Double> dblList = new ArrayList<Double>();        ArrayList<? extends Number> c2 = dblList;
    dblList.add(new Double(2.0));
    // T doesn't work here
    //ArrayList<T extends Number> c = intList;
                                                            We see that ArrayList<Integer> is a subtype of
    ArrayList<? extends Number> c = intList;
    ArrayList<? extends Number> c2 = dblList;                           ArrayList<? extends Number>
    Number x = c.get(0);
    //      c.add(new Integer(1));
    //      c.add(new Integer(2));
}




void printCollection(Collection c) {                        <T> void printCollection(Collection<T> c) {
 for (Object e : c) {                                         for (Object e : c) {
   System.out.println(e);                                       System.out.println(e);
  }                                                           }
}                                                           }
void printCollection(Collection<Object> c) {                void printCollection(Collection<?> c) {
  for (Object e : c) {                                        for (Object e : c) {
    System.out.println(e);                                      System.out.println(e);
  }                                                           }
}                                                           }
void printCollection(Collection<?> c) {                     <T> void printCollection(Collection<T> c) {
  for (Object e : c) {                                        for (T e : c) {
    System.out.println(e);                                      System.out.println(e);
  }                                                           }
}                                                           }




static <T> void fromArrayToCollection(T[] a,                Object[] oa = new Object[100];
                             Collection<T> c)               Collection<Object> co = new ArrayList<Object>();
{                                                           fromArrayToCollection(oa, co);// T inferred to be Object
  for (T o : a) {                                           String[] sa = new String[100];
    c.add(o); // correct                                    Collection<String> cs = new ArrayList<String>();
                                                            fromArrayToCollection(sa, cs);// T inferred to be String
  }
                                                            fromArrayToCollection(sa, co);// T inferred to be Object
}
                                                            Integer[] ia = new Integer[100];
                                                            Float[] fa = new Float[100];
                                                            Number[] na = new Number[100];
                                                            Collection<Number> cn = new ArrayList<Number>();
                                                            fromArrayToCollection(ia, cn);// T inferred to be Number
                                                            fromArrayToCollection(fa, cn);// T inferred to be Number
                                                            fromArrayToCollection(na, cn);// T inferred to be Number
                                                            fromArrayToCollection(na, co);// T inferred to be Object
                                                            fromArrayToCollection(na, cs);// compile-time error




                                                                                                                         4
interface Collection<E> {                                 // generics with legacy “raw” classes
  public boolean containsAll(Collection<?> c);            // generates “unchecked or unsafe operations”
  public boolean addAll(Collection<? extends E> c);       import java.util.*;
}
interface Collection<E> {                                 class Fiddle {
  public <T> boolean containsAll(Collection<T> c);          public static void main(String[] args) {
  public <T extends E> boolean addAll(Collection<T> c);       Collection<String> strings = new ArrayList<String>();
}                                                             Collection<Number> other;
                                                              other = foo(strings);
                                                            }
                                                            static Collection foo(Collection c) {
                                                              return c;
                                                            }
                                                          }




// generics with legacy “raw” classes                     List <String> l1 = new ArrayList<String>();
// generates “unchecked or unsafe operations”             List<Integer> l2 = new ArrayList<Integer>();
import java.util.*;                                       System.out.println(l1.getClass() == l2.getClass());

class Fiddle {
  public static void main(String[] args) {
    Collection raw = new ArrayList();
    Collection raw2;
    raw2 = bar(raw);
  }
  static <T> Collection<T> bar(Collection<T> c) {
    return c;
  }
}




<T> T badCast(T t, Object o) {
    return (T) o; // unchecked warning
}




                                                                                                                      5

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:12
posted:7/11/2011
language:English
pages:5