7

I got a strange compiler error when using generics within a for-each loop in Java. Is this a Java compiler bug, or am I really missing something here?

Here is my whole class:

public class Generics<T extends Object> {
  public Generics(T myObject){
    // I didn't really need myObject
  }

  public List<String> getList(){
    List<String> list = new ArrayList<String>();
    list.add("w00t StackOverflow");
    return list;
  }

  public static void main(String...a){
    Generics generics = new Generics(new Object());
    for(String s : generics.getList()){
      System.out.println(s);
    }
  }
}

The compiler is complaining about the line with the for-each: "Type mismatch cannot convert from element type Object to String."
If I make this subtle change, it compiles:

public static void main(String...a){
  Generics<?> generics = new Generics(new Object());
  for(String s : generics.getList()){
    System.out.println(s);
  }
}

I know getList() does use generics, but it uses them in what I thought was a completely unrelated way. I could understand this if I were trying to iterate over something of type T and getList() returned a List<T> or something, but that's not the case here. The return type of getList() should have absolutely nothing to do with T and shouldn't care whether I use the raw type for my Generics object or not...right? Shouldn't these be completely unrelated, or am I really missing something here?

Note that the code also compiles if I do this, which I thought should have been equivalent to the first as well:

public static void main(String...a){
  Generics generics = new Generics(new Object());
  List<String> list = generics.getList();
  for(String s : list){
    System.out.println(s);
  }
}
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Michael McGowan
  • 6,528
  • 8
  • 42
  • 70
  • 1
    `` is no different than ``. You are not making a generic version of you class, you are making the raw type. Which brings us to the question of why is your class generic in the first place? The only place you use T is in the constructor and you don't use that reference. – unholysampler Mar 25 '11 at 18:45
  • I used `` because I just needed something for an example. The real code obviously is something else, and it does use T...it just uses T in a way completely unrelated to `getList()`. – Michael McGowan Mar 25 '11 at 18:49
  • unrelated to your question, but i'd make the constructor be Generics cls) so that you don't have to instantiate an object of type T just to construct this Generics class. – MeBigFatGuy Mar 25 '11 at 19:55
  • I really just wanted to demonstrate `` without putting the focus on what that something was, so I picked `Object` for my example. – Michael McGowan Mar 25 '11 at 20:01

3 Answers3

12

The difference is that when you use the raw type, all the generic references within the member signatures are converted to their raw forms too. So effectively you're calling a method which now has a signature like this:

List getList()

Now as for why your final version compiles - although it does, there's a warning if you use -Xlint:

Generics.java:16: warning: [unchecked] unchecked conversion
    List<String> list = generics.getList();
                                        ^

This is similar to:

 List list = new ArrayList();
 List<String> strings = list;

... which also compiles, but with a warning under -Xlint.

The moral of the story: don't use raw types!

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I'm very surprised that *all* the generic references within the member signatures are converted to their raw forms. What is the reasoning for doing that (besides that Sun just felt like it)? – Michael McGowan Mar 25 '11 at 19:49
  • 4
    @Michael: The JLS includes this discussion in section 4.8 (raw types): "Raw types are closly related to wildcards. Both are based on existential types. Raw types can be thought of as wildcards whose type rules are deliberately unsound, to accommodate interaction with legacy code." In other words, raw types really shouldn't usually appear in new code, but they tried to avoid making old code fail to compile, even if it was at least suspicious. – Jon Skeet Mar 25 '11 at 19:51
  • Very interesting. I already knew to avoid using raw types (a coworker wrote the code to declare the variable), but this highlights that it really can matter. – Michael McGowan Mar 25 '11 at 20:04
  • @michael-mcgowan I guess at one point they got really tired with the immense details of generics, and this is where they think they can cut corners and save some of their time. Had they had more time/energy, they wouldn't leave it in such a sloppy state. – irreputable Mar 25 '11 at 21:09
3

Change the line

Generics generics = new Generics(new Object());

to

Generics<?> generics = new Generics<Object>(new Object());

The root of your problem is that you are using a raw type so the type of the getList method is List, not List<String>.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • Generics is NOT going to be generic of type String...that's the whole point. String is unrelated to the type of Generics. Regardless of T the `getList()` should return a `List`. – Michael McGowan Mar 25 '11 at 18:50
  • @Michael McGowan, good point. But there has to be some type associated with the type parameter at the point of declaration. `Generics> generics = new Generics(...);` would be fine, modulo an unsafe conversion warning. – Mike Samuel Mar 25 '11 at 19:01
  • @Michael McGowan, note that if all you did was remove the type parameter `` from the class declaration, then it would work. – Mike Samuel Mar 25 '11 at 19:03
-1

I made a couple adjustments to your code. You see in your comment you don't need Object in your constructor, so lets remove that to avoid any confusion. Second, if Generics is going to be generic, initialize it properly

Here is what the new main would look like

public static void main(String...a){
    Generics<String> generics = new Generics<String>();
    for(String s : generics.getList()){
      System.out.println(s);
    }
  }
Sean
  • 7,597
  • 1
  • 24
  • 26
  • Generics is NOT going to be generic of type String...that's the whole point. String is unrelated to the type of Generics. – Michael McGowan Mar 25 '11 at 18:48
  • I think you misunderstood my point. If you look at the code, you have getList() method returning a List. If we wanted to make the code really clean we could have stripped the generics part from the code other then in the getList() method. you were asking for help to get this to compile rather then was his approach right/wrong. – Sean Mar 25 '11 at 19:22
  • If you leave the generic deceleration on the class level, you open up the getList() method to remove the hard coding that exists now. – Sean Mar 25 '11 at 19:29