Results 1 to 4 of 4

Thread: Type Erasure

  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default 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:
    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:
    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
            }
        }
    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.
    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?
    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;
         }
    Any help is appreciated. Thanks in advance!
    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)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default

    Would this do it:

    public T getValue() {
    return value;
    }

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  1. What type of hang? and how to fix?
    By joshbrigade in forum New To Java
    Replies: 2
    Last Post: 11-06-2009, 10:40 PM
  2. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM
  3. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  4. The return type
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •