2

I just have a general question about generics. I've used them for years and heard about how they are not perfect because they are stripped of their type at compile time (correct?). I am having a hard time finding examples or explanations of particular code that causes them to fail. Can anybody give a code example and/or explain?

casperOne
  • 73,706
  • 19
  • 184
  • 253
smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113
  • 1
    The term you're looking for is called [type](http://download.oracle.com/javase/tutorial/java/generics/erasure.html) [erasure](http://en.wikipedia.org/wiki/Generics_in_Java#Type_erasure). However, I don't see an actual question in this. What do you mean by _"causes [generics] to fail?"_ – Matt Ball Dec 16 '10 at 17:14
  • @Matt I think he's looking for an example where type erasure causes a problem/bug. – Poindexter Dec 16 '10 at 17:16
  • Yeah I am still looking for a specific example where the compiler doesn't stop me from adding the wrong type to a collection. Can someone give an example of this? – smuggledPancakes Dec 16 '10 at 17:39
  • I don't think you will ever find such a case. Type erasure may introduce some inconvenience and tricky situations, but not obvious bugs like that. – Sergei Tachenov Dec 16 '10 at 18:12
  • There are cases wherein specific generic constructs compiles fine on one compiler, but not in other compiler. Do you mean this? – BalusC Dec 16 '10 at 18:30
  • @BalusC What? Could you please give an example? Is it a bug in the compiler or something that is not clearly defined by the language specification? – Sergei Tachenov Dec 16 '10 at 18:50
  • @Sergey: usually a bug in compiler. See [this](http://stackoverflow.com/questions/2858799/generics-compiles-and-runs-in-eclipse-but-doesnt-compile-in-javac) and [this](http://stackoverflow.com/questions/3000177/compilers-behave-differently-with-a-null-parameter-of-a-generic-method). – BalusC Dec 16 '10 at 18:53
  • I thought there were situations where you could write code, have it compile fine, but then have runtime exceptions where you put the wrong type of class in a collection and then called a method that didn't exist on that class. – smuggledPancakes Dec 16 '10 at 19:14

4 Answers4

5

What you are referring to is Type Erasure, where the compiler removes all information related to type parameters and type arguments within a class or method. An example where this can be a detriment is:

ArrayList<String> stringList = new ArrayList<String>();
ArrayList<Integer> integerList = new ArrayList<Integer>();

System.out.println(stringList.getClass().
    isAssignableFrom(integerList.getClass()));

We would hope this would print false, but it in fact prints true. There is no class distinction between stringList and integerList.

casperOne
  • 73,706
  • 19
  • 184
  • 253
1

The thing is pretty straightforward. In Java, Generics are implemented as a compiler feature, not a bytecode feature.

So, when compiler finds something like this:

public List<String> getStrings() { 
    return new ArrayList<String>();
}

It translates to bytecode code that does not know anything about generics.

So, something you lack is the ability to infer generic information when you do reflection on the class that has that method.

On the other hand, in .NET framework the language (C#) and the runtime know about generic.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
1

This code will for instance fail due to erasure:

if (someList instanceof List<String>)
    someList.add(myString);

The instanceof expression is evaluated at runtime, at which point the type parameter of someList is no longer available.

aioobe
  • 413,195
  • 112
  • 811
  • 826
-1

What cases have you seen where generics would fail (at runtime i presume)? I'm not sure if a generic object would have the chance to fail - my first guess is that this would be caught by the compiler, thus telling you that there is a type mismatch or whatever since Generics ensure type-safety.

http://msdn.microsoft.com/en-us/library/ms172192.aspx

AlvinfromDiaspar
  • 6,611
  • 13
  • 75
  • 140