7 Replies Latest reply: Jun 26, 2009 2:02 PM by 800285 RSS

    About Generic data type

    843789
      LinkedList<Integer> i=new LinkedList();


      How is it compile properly ?
        • 1. Re: About Generic data type
          843789
          what a differenece between

          HashMap hmap=new HashMap<Integer,String>();

          and


          HashMap<Integer,String> hmap=new HashMap<Integer,String>();

          and

          HashMap<Integer,String>hmap=new HashMap();
          • 2. Re: About Generic data type
            843789
            The third won't compile, the first can lead to nasty surprises at runtime, and the second is correct.
            • 3. Re: About Generic data type
              843789
              No everything is run properly.

              HashMap hmap1=new HashMap<Integer,String>();

                        HashMap<Integer,String> hmap2=new HashMap<Integer,String>();

                        HashMap<Integer,String>hmap3=new HashMap();
                        
                        for(int i=0;i<2;i++)
                        {
                             hmap1.put(i," "+Integer.toString(i));
                             hmap2.put(i," "+Integer.toString(i));
                             hmap3.put(i," "+Integer.toString(i));
                             
                        }
                        
                        
                        for(int i=0;i<2;i++)
                        {
                             System.out.println("hmap1 "+hmap1.put(i,Integer.toString(i)));
                             System.out.println("hmap2 "+hmap1.put(i,Integer.toString(i)));
                             System.out.println("hmap3 "+hmap1.put(i,Integer.toString(i)));
                             
                                       
                        }





              output

              hmap1 0
              hmap2 0
              hmap3 0
              hmap1 1
              hmap2 1
              hmap3 1
              • 4. Re: About Generic data type
                843789
                Mrjavan wrote:
                No everything is run properly.
                Yes, but the compiler gives you a warning about the third line:
                $ javac Foo.java 
                Note: Foo.java uses unchecked or unsafe operations.
                Note: Recompile with -Xlint:unchecked for details.
                $ javac -Xlint:unchecked Foo.java
                Foo.java:8: warning: [unchecked] unchecked conversion
                found   : java.util.HashMap
                required: java.util.HashMap<java.lang.Integer,java.lang.String>
                    HashMap<Integer,String>hmap3=new HashMap();
                                                 ^
                1 warning
                $
                To summarize:

                In the first line, you declare the full parameterized type in the instanciation, but discard it because your variable only has the raw type. So you're doing useless work.

                In the second line you declare the full parameterized type in both the variable declaration and the instanciation. That's the correct way. Now the compiler can check access to hmap2 for type correctness.

                In the third line you declare the full parameterized type in the variable declaration, but use a raw type for the instanciation. Here the compiler gives you a warning that it can't check the assignment at runtime. Don't do that.

                Note that at runtime all 3 will behave exactly the same, because of type erasure (google that term).
                • 5. Re: About Generic data type
                  843789
                  hi thank you .but i can't understood below point

                  "*Note that at runtime all 3 will behave exactly the same, because of type erasure (google that term).*"
                  • 6. Re: About Generic data type
                    843789
                    Mrjavan wrote:
                    hi thank you .but i can't understood below point

                    "*Note that at runtime all 3 will behave exactly the same, because of type erasure (google that term).*"
                    Let me rephrase that:

                    The only differences between the 3 versions are at compile time. They behave the same when the code is actually executed (i.e. at runtime). Use your favorite search engine (with an implicit suggestion of Google) to find more about the term "type erasure".
                    • 7. Re: About Generic data type
                      800285
                      Mrjavan wrote:
                      hi thank you .but i can't understood below point

                      "*Note that at runtime all 3 will behave exactly the same, because of type erasure (google that term).*"
                      I just finished reading the Generics chapter of my book :). Type erasure is the removal of all generics from your code (which has many complicated repercussions). Thats what Java does when it compiles you code. It's only the compiler that understands generics. As far as the byte code is concerned, generics doesn't exist.

                      So this code:
                      HashMap hmap1=new HashMap<Integer,String>();
                      HashMap<Integer,String> hmap2=new HashMap<Integer,String>();
                      HashMap<Integer,String>hmap3=new HashMap();
                      Integer i = hmap2.get("foo");
                      Will be converted to this code:
                      HashMap hmap1=new HashMap();
                      HashMap hmap2=new HashMap();
                      HashMap hmap3=new HashMap();
                      Integer i = (Integer)hmap2.get("foo");