Results 1 to 4 of 4
Thread: Type Erasure
- 06-29-2010, 01:13 AM #1
Type Erasure
I started reading about Generics again and I thought I was almost done with it until the last section: Type Erasure ( Type Erasure (The Java™ Tutorials > Learning the Java Language > Generics) )
So it states:
Okay that kind of make sense. The E in Box<E> is removed as String in Box<String> is removed. But then, how does the compiler know what to return?For instance, Box<String> is translated to type Box, which is called the raw type — a raw type is a generic class or interface name without any type arguments. This means that you can't find out what type of Object a generic class is using at runtime. The following operations are not possible:
The operations shown in bold are meaningless at runtime because the compiler removes all information about the actual type argument (represented by the type parameter E) at compile time.Java Code:public class MyClass<E> { public static void myMethod(Object item) { if (item instanceof E) { //Compiler error ... } E item2 = new E(); //Compiler error E[] iArray = new E[10]; //Compiler error E obj = (E)new Object(); //Unchecked cast warning } }
Any help is appreciated. Thanks in advance!Java Code:public class Example<T> { private T value; public Example(T value) { this.value = value; //When the type erasure removes the Ts, what does it replace it with? If it replaces it with Object, shouldn't item instanceOf E work then? } //works public Object getValue() { return value; } //compile-time error. public Number getValue() { return value; }Last edited by Lil_Aziz1; 06-29-2010 at 01:16 AM.
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-29-2010, 02:41 AM #2
Would this do it:
public T getValue() {
return value;
}
- 06-29-2010, 03:04 AM #3
Well yes, but the question is: if type erasure removes all information about a generic variable (private T t), how does it know what it is? If it replaces it with Object, why can't we do
Java Code:if (item instanceof E)
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-29-2010, 08:57 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Generics have to cooperate with legacy code; Generics only exist during compile time and constructs such as E item= new E() would force the compiler to call a constructor of an actual (raw) class; the compiler knows what constructor from which class it should call but it can't call it because you might fill in another real class for <E> at a new compilation and that would change the entire generated code (that's what C++ does and it results in the much hated 'code bloat').
When an E is returned from, say, a Collection<E> the compiler knows that and can check it and let the JVM simply return an Object; the compiler knew during compile time that it was a E because it checked it. (if it wasn't an E the compiler would've whined and stopped the compilation process).
kind regards,
Jos
Similar Threads
-
What type of hang? and how to fix?
By joshbrigade in forum New To JavaReplies: 2Last Post: 11-06-2009, 10:40 PM -
using instanceof to get Object type and parent type?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-06-2008, 06:24 PM -
[SOLVED] Cast string type to int type
By GilaMonster in forum New To JavaReplies: 9Last Post: 09-17-2008, 10:43 AM -
The return type
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 06:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks