From: Vikram on 6 Jun 2010 07:02 Hi, In java generics, the following code gives compile time error. List<Object> list = new ArrayList<String>(); // compile time error Where as the following does not give any compile time error List list = new ArrayList<String>(); If no generics is specified, isin't it implied that it contains object? What is the reason the second statement does not give any compile time error?
From: Tom Anderson on 6 Jun 2010 08:24 On Sun, 6 Jun 2010, Vikram wrote: > In java generics, the following code gives compile time error. > > List<Object> list = new ArrayList<String>(); // compile time error > > Where as the following does not give any compile time error > > List list = new ArrayList<String>(); > > If no generics is specified, isin't it implied that it contains object? No. If it's omitted, it's what's called a 'raw' type, which means generics isn't used at all. That second list could contain anything. It would be legal to write: list.add("one"); list.add(2); list.add(3.14159); See: http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#110257 tom -- Everything that has transpired has done so according to my design.
From: Eric Sosman on 6 Jun 2010 08:33 On 6/6/2010 7:02 AM, Vikram wrote: > Hi, > > In java generics, the following code gives compile time error. > > List<Object> list = new ArrayList<String>(); // compile time error I'll rewrite this slightly, to make it easy to refer to the left-hand and right-hand sides independently: List<String> slist = new ArrayList<String>(); List<Object> olist = slist; // compile error When we fetch something from slist, we know that we'll get a String, right? That's the purpose of the <String> notation: It tells the compiler to complain if we try to put a non-String into slist. Since the compiler will ensure that we never put anything except Strings into slist, we know we will get only Strings out. Suppose, though, that the second line were legal, and that we could get olist to refer to the same ArrayList object as slist. Now, we could do olist.add("forty-two"); olist.add(new Integer(42)); olist.add(new Double(42.0); olist.add(new Color(42, 42, 42)); olist.add(new JLabel("42")); .... since all of the things we are adding are Objects, and hence are acceptable to the <Object> notation of olist. Clear? But olist and slist refer to the same ArrayList, so what happens when we start fetching things from slist? We get all the things that were added through olist. Are they all Strings? Obviously not. What next? ClassCastException -- the very run-time error generics were invented to prevent. > Where as the following does not give any compile time error > > List list = new ArrayList<String>(); Here, the `list' List makes no guarantees. The compiler will allow you to add anything at all via the `list' reference. Also, the compiler will not assume that everything you fetch from `list' is a String. By omitting all <> notation, you are telling the compiler that all bets are off: You and you alone are responsible for what goes into and what comes out of the List, and the compiler will not try to prevent you from doing something silly. > If no generics is specified, isin't it implied that it contains > object? What is the reason the second statement does not give any > compile time error? Mostly for interoperability with Java code written before generics came along. Suppose you are using somebody else's class, written in the pre-generic days, that has a method like /** Do something to a List of Strings. * @param list A List containing Strings. * @throws ClassCastException if "list" contains any * non-Strings. */ void doSomething(List list) { for (Iterator it = list.iterator(); it.hasNext(); ) { String s = (String)it.next(); ... } } You, being a modern and forward-looking person, are using generics in your own code, and since you need a List containing Strings and nothing but Strings, you've built a List<String>. Now, you'd like to call this other class' doSomething() method on your List<String>, but doSomething() takes a plain List. Clearly, your List<String> meets all the requirements of the List that doSomething() wants, so this should be allowed. And that, more or less, is why it's all right to use a "decorated" generic object where an "undecorated" non-generic version is called for. The on-line Java Tutorial has a section on generics that you might find helpful: <http://java.sun.com/docs/books/tutorial/java/generics/index.html> -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: Lew on 6 Jun 2010 09:48 Vikram wrote: >> Where as the following does not give any compile time error >> >> List list = new ArrayList<String>(); Really? No error? What about the "unchecked" warning, then? Hm? Because I get errors (well, ok, warnings, but a warning is a flavor of error) about the use of a "raw type conversion" if I try that construct. >> If no generics is specified, isin't it implied that it contains >> object? What is the reason the second statement does not give any >> compile time error? "Foo extends Bar" does not mean "List <Foo> extends List <Bar>". This is explained in detail in every generics tutorial and reference. Which of course you've read. They then go on to explain that generics wildcards ("?" notation) resolve this, sort of. <http://java.sun.com/docs/books/tutorial/java/generics/subtyping.html> & ff. Eric Sosman wrote: > The on-line Java Tutorial has a section on generics that you > might find helpful: > > <http://java.sun.com/docs/books/tutorial/java/generics/index.html> Since you've read this tutorial thoroughly and it hasn't answered all our question, now read the free chapter (ch. 5) on generics from Joshua Bloch's /Effective Java/, available as a PDF download from <http://java.sun.com/docs/books/effective/java> <http://www.google.com/search?q=Java+generics+introduction> is another good resource. AMong other sources, that will steer you to <http://www.angelikalanger.com/Articles/JavaPro/01.JavaGenericsIntroduction/JavaGenerics.html> -- Lew
From: Kevin McMurtrie on 6 Jun 2010 13:18 In article <alpine.DEB.1.10.1006061324340.6621(a)urchin.earth.li>, Tom Anderson <twic(a)urchin.earth.li> wrote: > On Sun, 6 Jun 2010, Vikram wrote: > > > In java generics, the following code gives compile time error. > > > > List<Object> list = new ArrayList<String>(); // compile time error > > > > Where as the following does not give any compile time error > > > > List list = new ArrayList<String>(); > > > > If no generics is specified, isin't it implied that it contains object? > > No. If it's omitted, it's what's called a 'raw' type, which means generics > isn't used at all. That second list could contain anything. It would be > legal to write: > > list.add("one"); > list.add(2); > list.add(3.14159); > > See: > > http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#110257 > > tom Java sometimes adds a non-generics method like this: public void add (Object foo) { add((SomeClass)foo); } It can lead to ClassCastException on nonexistent lines. -- I won't see Google Groups replies because I must filter them as spam
|
Next
|
Last
Pages: 1 2 3 Prev: logging into site using form validation http components Next: documentation versus contract |