Results 1 to 4 of 4
  1. #1
    dongyi_he is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Generic Questions

    Hi guys,

    I am just a little curious about the difference between

    List<String> list = new LinkedList<String>() and
    List<String> list2 = new LinkedList();

    It seems complier will check the type of objects added in both list and list2. Also Because Java complier use Type Erasure and there is no type information for objects inside both lists at run time. So they are the same?

    Could anyone helps? Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I don't know the canonical answer, but if you ever update your code and pass a parameter to the constructors, the generic one will do type checking but the non-generic one will not. For example:

    Java Code:
          List<Integer> intList = new LinkedList<Integer>();
          intList.add(4);
          intList.add(6);
    
          List<String> list2 = new LinkedList(intList);  // this is legal, senseless, but legal
          List<String> list = new LinkedList<String>(intList); // this is not

  3. #3
    dongyi_he is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    En...It is very interesting, many thanks Fubarable!

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    I don't know the canonical answer, but if you ever update your code and pass a parameter to the constructors, the generic one will do type checking but the non-generic one will not. For example:

    Java Code:
          List<Integer> intList = new LinkedList<Integer>();
          intList.add(4);
          intList.add(6);
    
          List<String> list2 = new LinkedList(intList);  // this is legal, senseless, but legal
          List<String> list = new LinkedList<String>(intList); // this is not
    The list2 initialization does cause a compiler warning (and rightly so). IMHO generics are a mixed blessing: I like the fact that it doesn't cause "code bloat" as it can do in C++ but I dislike the erasure thingy (which is a consequence of the design of it all). I also dislike the code backwards compatibility of allowing "raw" types which effectively annihilates even compile time checks.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. generic code
    By mac in forum New To Java
    Replies: 4
    Last Post: 05-27-2010, 05:06 PM
  2. generic types
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 06-12-2009, 10:29 PM
  3. Generic methods
    By andre1011 in forum Advanced Java
    Replies: 7
    Last Post: 02-25-2009, 02:17 PM
  4. Generic array
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 12:12 AM
  5. Generic Hashtables
    By ShoeNinja in forum New To Java
    Replies: 0
    Last Post: 12-04-2007, 10:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •