Results 1 to 5 of 5
  1. #1
    La VloZ is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Question Generics Question

    Hello all
    How are you all??

    I have a little question for generics
    see these two examples

    Java Code:
    public static void printList(List<?> list){  
         for(Object elem : list){  
              System.out.println(elem);  
         }  
    }
    and

    Java Code:
    public static <T extends Object>void printList(List<T> list){  
         for(Object elem : list){  
              System.out.println(elem);  
         }  
    }
    they do the same thing i tried then
    i know that the first is non-generic method and the second is generic method (if i'm not wrong)
    but can someone tell me what's the diffrence between them which is the best practice and why??

    Thank you all

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,040
    Rep Power
    6

    Default Re: Generics Question

    Actually, they are both generic. They both result in the same erasure being applied but semantically they mean different things. The first says you have a list of unknown type. The second says you have a list of some type T which is a subtype of Object. I tend to prefer the first method since its a little clearer (and less typing which is always a good thing).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    La VloZ is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Default Re: Generics Question

    Quote Originally Posted by jim829 View Post
    Actually, they are both generic. They both result in the same erasure being applied but semantically they mean different things. The first says you have a list of unknown type. The second says you have a list of some type T which is a subtype of Object. I tend to prefer the first method since its a little clearer (and less typing which is always a good thing).

    Regards,
    Jim
    Ahhhhhh ok :)
    Thank you :)

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,371
    Rep Power
    6

    Default Re: Generics Question

    Thanks for cross-posting and not mentioning it by the way:

    https://forums.oracle.com/thread/2556618

  5. #5
    La VloZ is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Default Re: Generics Question

    Quote Originally Posted by gimbal2 View Post
    Thanks for cross-posting and not mentioning it by the way:

    https://forums.oracle.com/thread/2556618
    Hihihihihi ^___^, first i posted it on oracle forums :) but oracle forums are sleepy ZzZzZzZz :)
    so i posted here :) (i couldn't wait) ;)

Similar Threads

  1. Question on Generics
    By bimtownboy in forum New To Java
    Replies: 2
    Last Post: 05-01-2013, 03:24 PM
  2. Generics Question
    By erversteeg in forum New To Java
    Replies: 3
    Last Post: 02-11-2011, 06:13 AM
  3. Question regarding generics
    By Leaflord in forum Advanced Java
    Replies: 9
    Last Post: 08-29-2009, 03:24 PM
  4. Generics Question
    By jdgallag in forum New To Java
    Replies: 8
    Last Post: 10-28-2008, 07:15 PM
  5. Question about java generics
    By Arrowx7 in forum New To Java
    Replies: 1
    Last Post: 08-14-2008, 03:37 AM

Tags for this Thread

Posting Permissions

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