Forum Stats

  • 3,752,284 Users
  • 2,250,483 Discussions
  • 7,867,783 Comments

Discussions

Doubt on type erasure?

Murray9654
Murray9654 Member Posts: 486
edited Feb 27, 2013 9:08AM in Generics
Hi my doubt is about type erasure. The following is the class with generics.
public class Person<T>{
    
       public T add(T a){
               System.out.println("happy");
       }
}
When this is compiled type erasure happens. I mean when it is converted to class file type erasure happens. so the code could be as follows
public class Person{

         public Object add(Object a){
                    System.out.println("happy");
         }
}
once the compilation is finish, I started writing the main method and in my main method when i declare
Person a=new Person()
the compiler generates a warning saying that Person class is a raw type. arguments have to be provided for type parameters. So when there is no generics in the class file, how does the compiler identifies that Person is a generic class and arguments have to be provided for type parameters.

Best Answer

  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    edited Feb 27, 2013 7:41AM Accepted Answer
    Muralidhar wrote:
    Now there is a basic doubt. till now i thought byte code and the class file is the same. I mean class file contains the byte code that is the reason class file runs on all platforms. I also have googled to find out the difference between byte code and class file but i could not find any. Is class file different from byte code. If so what is class file and what is byte code?
    The class file contains the byte code indeed - among other pieces of information prudent to the virtual machine to be able to actually load and interpret the class file.

    http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html

    You should do a basic study into binary data formats to understand how applications work with those. Generally you need some additional information, which is usually stored at the top of the file (the file "header"), to know how to load and interpret the binary data in the file. Such header information could contain the generics details for example.

    Take an image file as another simpler example: its impossible to properly load the image data if you don't know at least how wide and heigh the image is, how many bytes a single pixel takes up, etc. so you can't just dump the binary image data in the file, you also need to store the additional header information.

Answers

  • EJP
    EJP Member Posts: 32,920 Gold Crown
    There are no generics in the byte-code. There is generics information elsewhere.
  • Murray9654
    Murray9654 Member Posts: 486
    What is that "else where"? brief idea about how compiler recognizes generic class?
  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    Muralidhar wrote:
    What is that "else where"? brief idea about how compiler recognizes generic class?
    From the class file of course.

    http://lmgtfy.com/?q=generic+information+in+class+file

    What is it with Google that makes it so hard to use?
  • Murray9654
    Murray9654 Member Posts: 486
    Then when does type erasure happens. It is discussed in oracle docs. I mean when class file contains generic information when does type erasure happens?
  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    Muralidhar wrote:
    Then when does type erasure happens. It is discussed in oracle docs. I mean when class file contains generic information when does type erasure happens?
    During the compiling. Bytecode has no generics information even if the class file does.
  • Murray9654
    Murray9654 Member Posts: 486
    Now there is a basic doubt. till now i thought byte code and the class file is the same. I mean class file contains the byte code that is the reason class file runs on all platforms. I also have googled to find out the difference between byte code and class file but i could not find any. Is class file different from byte code. If so what is class file and what is byte code?
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    edited Feb 27, 2013 7:36AM
    The .class contains byte-code and other information, such as method tables, constant pools, and the information the compiler uses that is the answer to your question. Is this really so mysterious?
    EJP
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    edited Feb 27, 2013 7:41AM Accepted Answer
    Muralidhar wrote:
    Now there is a basic doubt. till now i thought byte code and the class file is the same. I mean class file contains the byte code that is the reason class file runs on all platforms. I also have googled to find out the difference between byte code and class file but i could not find any. Is class file different from byte code. If so what is class file and what is byte code?
    The class file contains the byte code indeed - among other pieces of information prudent to the virtual machine to be able to actually load and interpret the class file.

    http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html

    You should do a basic study into binary data formats to understand how applications work with those. Generally you need some additional information, which is usually stored at the top of the file (the file "header"), to know how to load and interpret the binary data in the file. Such header information could contain the generics details for example.

    Take an image file as another simpler example: its impossible to properly load the image data if you don't know at least how wide and heigh the image is, how many bytes a single pixel takes up, etc. so you can't just dump the binary image data in the file, you also need to store the additional header information.
  • Murray9654
    Murray9654 Member Posts: 486
    It is clear now. Thank you so much.
This discussion has been closed.