9 Replies Latest reply: Feb 27, 2013 8:08 AM by Murray9654 RSS

    Doubt on type erasure?

    Murray9654
      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.
        • 1. Re: Doubt on type erasure?
          EJP
          There are no generics in the byte-code. There is generics information elsewhere.
          • 2. Re: Doubt on type erasure?
            Murray9654
            What is that "else where"? brief idea about how compiler recognizes generic class?
            • 3. Re: Doubt on type erasure?
              Kayaman
              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?
              • 4. Re: Doubt on type erasure?
                Murray9654
                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?
                • 5. Re: Doubt on type erasure?
                  Kayaman
                  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.
                  • 6. Re: Doubt on type erasure?
                    Murray9654
                    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?
                    • 7. Re: Doubt on type erasure?
                      EJP
                      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?
                      • 8. Re: Doubt on type erasure?
                        gimbal2
                        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.
                        • 9. Re: Doubt on type erasure?
                          Murray9654
                          It is clear now. Thank you so much.