This discussion is archived
8 Replies Latest reply: Feb 28, 2010 3:28 PM by EJP RSS

generics`

843793 Newbie
Currently Being Moderated
List list=new ArrayList<Integer>();
list.add(1);
list.add(new Integer(2));
list.add("3");
list.add("4");
//above gives no error

List<Integer> list=new ArrayList();
list.add(1);
list.add(new Integer(2));
list.add("3");
list.add("4");
//it gives error
why
  • 1. Re: generics`
    EJP Guru
    Currently Being Moderated
    Because "3" and "4" are Strings not Integers and you declared it as a List of Integer.
  • 2. Re: generics`
    843793 Newbie
    Currently Being Moderated
    Additionally, the first set of code gives no error because you assigned the list to the raw type List which allows any type (even though it was assigned from an instance of ArrayList<Integer>).
  • 3. Re: generics`
    Alan.M Newbie
    Currently Being Moderated
    Hi,
    In generic, you look at non run time type i.e in your case left side ( type safety during compilation time)

    List list=new ArrayList<Integer>(); // here is a List which takes any row type it is sa saying List<? extends Object>

    so the compiled type ( the one on the left ) accepts a list of any type which extends Object hence Integer and String all extends Object

    Now the second one

    List<Integer> list2=new ArrayList(); // you said here I want a list which accept only Integer ( you have to think about the none run time type which is on the left)
    There is a type safety now which you have introduced and it is called List<Integer> ; so the compiler will stop you if you try to enter non integer

    We have not discussed the wild card which makes things more confusing for you.



    Regards,
    Alan
    London
  • 4. Re: generics`
    EJP Guru
    Currently Being Moderated
    Alan, that's what I said a week ago in one sentence.
  • 5. Re: generics`
    Alan.M Newbie
    Currently Being Moderated
    EJB,
    Thanks for letting me know that I explained the same idea which you explained before; however, I just quickly browsed the tutorial from http://java.sun.com/docs/books/tutorial/java/generics and I build the conculsion based
    "type safety during compilation" and not during run time. to get my thoughts and not more then that.

    Deep apology from me.

    Cheers
    Alan
  • 6. Re: generics`
    EJP Guru
    Currently Being Moderated
    Alan, it's great that you want to contribute, but you do this all the time: you take an old thread and append some laborious and not necessarily accurate explanation of something that has already been answered. It's just wasting everybody's time.

    In this case the question is about a compile error, which happens at compile-time, so of course 'type safety during compilation' is the topic.

    If you want to contribute, do make sure you add some value.
  • 7. Re: generics`
    Alan.M Newbie
    Currently Being Moderated
    EJB,
    Sorry to say, your accusation is very harsh and not accurate and not acceptable. I never do what you describe.
  • 8. Re: generics`
    EJP Guru
    Currently Being Moderated
    I never do what you describe.
    You did it right here in this thread. Came in a week late with either a restatement or the wrong answer. I can cite further examples if you wish, but you should know your own posting history better than I do.