1

Consider the following code:

List<Integer>ints= new ArrayList<Integer>();
lst.add(new Object());//no suitable method found for add(Object)...

Why this error is causing? On a compile time we have type erasure, and method boolean add (E e) after erasure will have signature add(Object o). Can you write in detail how ompiler work in this case?

And what about bridge method? As i understood bridge metod have the following implements:

boolean add(Object o){return this.add((Integer) o)}
St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • You are trying to add an Object to a list of Integers! Why do you expect this to work? That's exactly what generics were made for. – isnot2bad Sep 26 '13 at 15:44
  • possible duplicate of [How covariant method overriding is implemented using bridging Technique in java](http://stackoverflow.com/questions/18655541/how-covariant-method-overriding-is-implemented-using-bridging-technique-in-java) – Rohit Jain Sep 26 '13 at 15:49
  • where is `lst` defined? – newacct Sep 30 '13 at 22:46

3 Answers3

7

Type erasure occurs after the compiler has perform type-checking. If it were the other way around, there would be no point in generics!

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

You aren't evaluating

lst.add(new Object()); 

at runtime, you are evaluating it at compile time. At compile time, there is no method List<Integer>#add(Object).

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

Bridge methods go in class definition, not in methods, example:

public class Test implements Comparable<Test> {

    public int compareTo(Test o) {
        return ...;
    }
...

compiler will add a bridge method (invisible) here

public int compareTo(Object o) {
    return compareTo((Test)o);
}

because Comparable interface in bytecode has int compareTo(Object o) method and for JVM to detect that the class implements this method the class needs int compareTo(Object o)

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 1
    It is `public int compareTo(Object o) { return compareTo((Test)o); }` Otherwise it wasn’t a “bridge”. It makes a difference if a subclass of `Test` overrides `compareTo(Test o)`. – Holger Sep 26 '13 at 17:14
  • Consider the following code: `List ints = new ArrayList(); ints.add(5):` What will be occur in this specific case if compiler doesnt generate a bridge method? Does bridge method generated after type-checked and before type erasure? – St.Antario Sep 26 '13 at 17:30
  • @St.Antario: **No**. Using a generic class will not create any bridge methods. So using an `ArrayList` creates no bridge methods. Look at the example above. It’s *implementing* a generic interface with bound type parameters. This is a situation where bridge methods are created. Completely different story than just instantiating a generic class. – Holger Sep 26 '13 at 18:22
  • @Holger Why no bridge method in my case? ArrayList is subclass of AbstractList. http://docs.oracle.com/javase/tutorial/java/generics/bridgeMethods.html#bridgeMethods _When compiling a class or interface that extends a parameterized class or implements a parameterized interface, the compiler may need to create a synthetic method, called a bridge method, as part of the type erasure process._ – St.Antario Sep 27 '13 at 04:42
  • *may* create bridge methods. Since `ArrayList` does not change the bounds of `AbstractList`’s type parameter `` there is no need for bridge methods. The raw type’s methods still use `Object` where the generic type has `E`. – Holger Sep 27 '13 at 07:56