Type erasure and bridge method
NickName:St.Antario Ask DateTime:2013-09-26T23:38:10

Type erasure and bridge method

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)}

Copyright Notice:Content Author:「St.Antario」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19032602/type-erasure-and-bridge-method

Answers
Oliver Charlesworth 2013-09-26T15:39:38

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


Sotirios Delimanolis 2013-09-26T15:39:46

You aren't evaluating\n\nlst.add(new Object()); \n\n\nat runtime, you are evaluating it at compile time. At compile time, there is no method List<Integer>#add(Object).",


More about “Type erasure and bridge method” related questions

Type erasure and bridge method

Consider the following code: List&lt;Integer&gt;ints= new ArrayList&lt;Integer&gt;(); lst.add(new Object());//no suitable method found for add(Object)... Why this error is causing? On a compile t...

Show Detail

Generics Bridge Method --- on Polymorphism

I am trying to understand the concept of Bridge Method creation, and was stuck on the example given on the Oracle Java Docs. Below are the example for reference Given are the following two classe...

Show Detail

Generic method bounded type parameter and type erasure

A generic method as per below: static &lt;E, K extends E&gt; void someMethod(K k, E[] e) {} I presumed upon erasure, the erasure type would be: static void someMethod(Object k, Object[] e) {} ...

Show Detail

Is it a bridge method?

Why it is possible to compile class with two methods with equal signatures? "foo" methods have only different return types. But return type is not a part of method signature in java. Does java comp...

Show Detail

Type erasure and overriding generic method

My understanding about generic was that if I have public void saveAll(Collection&lt;? extends Object&gt; stuff) { then compile will determine the "most common type" of the input parameter to be

Show Detail

Method overloading with type erasure

In the book "Java Generics and Collections" by Maurice Naftalin &amp; Philip Wadler there is a code related to method overloading with type erasure on page 56. It says that just like two methods w...

Show Detail

Java Generics - method after type erasure

How will the following method look after applying type erasure? static&lt;T&gt; void InsertAt0(List&lt;T&gt; mylist, T element) { mylist.add(0, element); } will T be replaced with Object? or ...

Show Detail

Method has the same erasure as another method in type - part 2

I completely get this question Method has the same erasure as another method in type and the answer to it. Please can anyone help me understand the below? Trying hard to digest, why the 2nd code s...

Show Detail

Type Erasure tutorial on Java

I am reading the Generics trail on Oracle (Type Erasure) and I was not able to understand the following part. The code snippets are shown below: public class Node&lt;T&gt; { public T data; ...

Show Detail

Erasure type and Bridging Method clarification

The code following is taken from Oracle documentation of generics - class Node&lt;T&gt; { public T data; public Node(T data) { this.data = data; } public void setData(T data) { ...

Show Detail