difference between referencing a parameterized type instance to its raw type and using a raw type to...











up vote
1
down vote

favorite












I have recently started learning Java from the basics and i run into this
"little" misunderstanding about generic types, and it raised a question as follow :



Which is the difference between referencing a parameterized type instance to its raw type and
using a raw type to reference another raw type instance?



I mean, which is the difference between this snippet:



ArrayList rawTypeList_NoRawInstance = new ArrayList</*Any type here*/>();


and this one:



ArrayList rawTypeList_RawInstance = new ArrayList();


Code:



import java.util.*;


public class TestGenerics{

public static void main(String args){


ArrayList rawTypeList_RawInstance = new ArrayList();
ArrayList rawTypeList_NoRawInstance = new ArrayList<Integer>(); /* instead of Integer could be placed any kind of type, this
* is just an example */

rawTypeList_RawInstance.add("example RawInstance"); // warning launched
rawTypeList_NoRawInstance.add("example NoRawInstance"); // same warning here

System.out.println(rawTypeList_RawInstance.get(0)); // content showed without errors/warning
System.out.println(rawTypeList_NoRawInstance.get(0)); // same here

String exampleRawInstance1 = (String)rawTypeList_RawInstance.get(0); // raw type instance compiled without error
String exampleNoRawInstance1 = (String)rawTypeList_NoRawInstance.get(0); // Generic type -Integer- instance compiled without error

Integer exampleRawInstance2 = (Integer)rawTypeList_RawInstance.get(0); // ClassCastException as expected
Integer exampleNoRawInstance2 = (Integer)rawTypeList_NoRawInstance.get(0); // same here, logically
}

}


Could anyone explain me the difference and bring me some examples about possible different consequences?










share|improve this question




























    up vote
    1
    down vote

    favorite












    I have recently started learning Java from the basics and i run into this
    "little" misunderstanding about generic types, and it raised a question as follow :



    Which is the difference between referencing a parameterized type instance to its raw type and
    using a raw type to reference another raw type instance?



    I mean, which is the difference between this snippet:



    ArrayList rawTypeList_NoRawInstance = new ArrayList</*Any type here*/>();


    and this one:



    ArrayList rawTypeList_RawInstance = new ArrayList();


    Code:



    import java.util.*;


    public class TestGenerics{

    public static void main(String args){


    ArrayList rawTypeList_RawInstance = new ArrayList();
    ArrayList rawTypeList_NoRawInstance = new ArrayList<Integer>(); /* instead of Integer could be placed any kind of type, this
    * is just an example */

    rawTypeList_RawInstance.add("example RawInstance"); // warning launched
    rawTypeList_NoRawInstance.add("example NoRawInstance"); // same warning here

    System.out.println(rawTypeList_RawInstance.get(0)); // content showed without errors/warning
    System.out.println(rawTypeList_NoRawInstance.get(0)); // same here

    String exampleRawInstance1 = (String)rawTypeList_RawInstance.get(0); // raw type instance compiled without error
    String exampleNoRawInstance1 = (String)rawTypeList_NoRawInstance.get(0); // Generic type -Integer- instance compiled without error

    Integer exampleRawInstance2 = (Integer)rawTypeList_RawInstance.get(0); // ClassCastException as expected
    Integer exampleNoRawInstance2 = (Integer)rawTypeList_NoRawInstance.get(0); // same here, logically
    }

    }


    Could anyone explain me the difference and bring me some examples about possible different consequences?










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have recently started learning Java from the basics and i run into this
      "little" misunderstanding about generic types, and it raised a question as follow :



      Which is the difference between referencing a parameterized type instance to its raw type and
      using a raw type to reference another raw type instance?



      I mean, which is the difference between this snippet:



      ArrayList rawTypeList_NoRawInstance = new ArrayList</*Any type here*/>();


      and this one:



      ArrayList rawTypeList_RawInstance = new ArrayList();


      Code:



      import java.util.*;


      public class TestGenerics{

      public static void main(String args){


      ArrayList rawTypeList_RawInstance = new ArrayList();
      ArrayList rawTypeList_NoRawInstance = new ArrayList<Integer>(); /* instead of Integer could be placed any kind of type, this
      * is just an example */

      rawTypeList_RawInstance.add("example RawInstance"); // warning launched
      rawTypeList_NoRawInstance.add("example NoRawInstance"); // same warning here

      System.out.println(rawTypeList_RawInstance.get(0)); // content showed without errors/warning
      System.out.println(rawTypeList_NoRawInstance.get(0)); // same here

      String exampleRawInstance1 = (String)rawTypeList_RawInstance.get(0); // raw type instance compiled without error
      String exampleNoRawInstance1 = (String)rawTypeList_NoRawInstance.get(0); // Generic type -Integer- instance compiled without error

      Integer exampleRawInstance2 = (Integer)rawTypeList_RawInstance.get(0); // ClassCastException as expected
      Integer exampleNoRawInstance2 = (Integer)rawTypeList_NoRawInstance.get(0); // same here, logically
      }

      }


      Could anyone explain me the difference and bring me some examples about possible different consequences?










      share|improve this question















      I have recently started learning Java from the basics and i run into this
      "little" misunderstanding about generic types, and it raised a question as follow :



      Which is the difference between referencing a parameterized type instance to its raw type and
      using a raw type to reference another raw type instance?



      I mean, which is the difference between this snippet:



      ArrayList rawTypeList_NoRawInstance = new ArrayList</*Any type here*/>();


      and this one:



      ArrayList rawTypeList_RawInstance = new ArrayList();


      Code:



      import java.util.*;


      public class TestGenerics{

      public static void main(String args){


      ArrayList rawTypeList_RawInstance = new ArrayList();
      ArrayList rawTypeList_NoRawInstance = new ArrayList<Integer>(); /* instead of Integer could be placed any kind of type, this
      * is just an example */

      rawTypeList_RawInstance.add("example RawInstance"); // warning launched
      rawTypeList_NoRawInstance.add("example NoRawInstance"); // same warning here

      System.out.println(rawTypeList_RawInstance.get(0)); // content showed without errors/warning
      System.out.println(rawTypeList_NoRawInstance.get(0)); // same here

      String exampleRawInstance1 = (String)rawTypeList_RawInstance.get(0); // raw type instance compiled without error
      String exampleNoRawInstance1 = (String)rawTypeList_NoRawInstance.get(0); // Generic type -Integer- instance compiled without error

      Integer exampleRawInstance2 = (Integer)rawTypeList_RawInstance.get(0); // ClassCastException as expected
      Integer exampleNoRawInstance2 = (Integer)rawTypeList_NoRawInstance.get(0); // same here, logically
      }

      }


      Could anyone explain me the difference and bring me some examples about possible different consequences?







      java generics type-erasure raw-types






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 22:41









      rgettman

      147k21201286




      147k21201286










      asked Nov 9 at 22:36









      DaviLevi

      82




      82
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Generics are only present at compile time, they will be removed by the compiler (this is called type erasure). They are there for giving some type information to the compiler. This helps you avoiding type casts (as usual before Java 1.5) and allows the compiler for more type checking. It is also a valuable information to the programmer, for example if you see a generic type in an interface.



          Without generics:



          ArrayList list = new ArrayList();


          So, it would make a difference if you write:



          ArrayList</*Any type here*/> list = new ArrayList<>();


          Now the compiler has the information what type of objects are in list.



          But this one makes no real difference to the version without generics:



          ArrayList list = new ArrayList</*Any type here*/>();


          The variable list has no generics information accompanied, so it is as good (or bad) as the version without generics.






          share|improve this answer





















          • Yea, i get the point here.@Donat In fact, the java compiler should provide a type cast as described in the type erasure section on Oracle documentation. "Insert type casts if necessary to preserve type safety" source : docs.oracle.com/javase/tutorial/java/generics/erasure.html So, it means that just the reference/variable "list" could graft on this kind of type cast from the compilator? So, in this case: 'ArrayList list = new ArrayList</*Any type here*/>();' An instance, on the other hand could not? because otherwise it should provide a typesafe on the object pointed
            – DaviLevi
            Nov 10 at 16:00












          • Yes, the generic type information is attached to the variable and is there only at compile time. The instance exists at runtime and then the type infrmation has been removed.
            – Donat
            Nov 10 at 16:07











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234142%2fdifference-between-referencing-a-parameterized-type-instance-to-its-raw-type-and%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          Generics are only present at compile time, they will be removed by the compiler (this is called type erasure). They are there for giving some type information to the compiler. This helps you avoiding type casts (as usual before Java 1.5) and allows the compiler for more type checking. It is also a valuable information to the programmer, for example if you see a generic type in an interface.



          Without generics:



          ArrayList list = new ArrayList();


          So, it would make a difference if you write:



          ArrayList</*Any type here*/> list = new ArrayList<>();


          Now the compiler has the information what type of objects are in list.



          But this one makes no real difference to the version without generics:



          ArrayList list = new ArrayList</*Any type here*/>();


          The variable list has no generics information accompanied, so it is as good (or bad) as the version without generics.






          share|improve this answer





















          • Yea, i get the point here.@Donat In fact, the java compiler should provide a type cast as described in the type erasure section on Oracle documentation. "Insert type casts if necessary to preserve type safety" source : docs.oracle.com/javase/tutorial/java/generics/erasure.html So, it means that just the reference/variable "list" could graft on this kind of type cast from the compilator? So, in this case: 'ArrayList list = new ArrayList</*Any type here*/>();' An instance, on the other hand could not? because otherwise it should provide a typesafe on the object pointed
            – DaviLevi
            Nov 10 at 16:00












          • Yes, the generic type information is attached to the variable and is there only at compile time. The instance exists at runtime and then the type infrmation has been removed.
            – Donat
            Nov 10 at 16:07















          up vote
          0
          down vote



          accepted










          Generics are only present at compile time, they will be removed by the compiler (this is called type erasure). They are there for giving some type information to the compiler. This helps you avoiding type casts (as usual before Java 1.5) and allows the compiler for more type checking. It is also a valuable information to the programmer, for example if you see a generic type in an interface.



          Without generics:



          ArrayList list = new ArrayList();


          So, it would make a difference if you write:



          ArrayList</*Any type here*/> list = new ArrayList<>();


          Now the compiler has the information what type of objects are in list.



          But this one makes no real difference to the version without generics:



          ArrayList list = new ArrayList</*Any type here*/>();


          The variable list has no generics information accompanied, so it is as good (or bad) as the version without generics.






          share|improve this answer





















          • Yea, i get the point here.@Donat In fact, the java compiler should provide a type cast as described in the type erasure section on Oracle documentation. "Insert type casts if necessary to preserve type safety" source : docs.oracle.com/javase/tutorial/java/generics/erasure.html So, it means that just the reference/variable "list" could graft on this kind of type cast from the compilator? So, in this case: 'ArrayList list = new ArrayList</*Any type here*/>();' An instance, on the other hand could not? because otherwise it should provide a typesafe on the object pointed
            – DaviLevi
            Nov 10 at 16:00












          • Yes, the generic type information is attached to the variable and is there only at compile time. The instance exists at runtime and then the type infrmation has been removed.
            – Donat
            Nov 10 at 16:07













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Generics are only present at compile time, they will be removed by the compiler (this is called type erasure). They are there for giving some type information to the compiler. This helps you avoiding type casts (as usual before Java 1.5) and allows the compiler for more type checking. It is also a valuable information to the programmer, for example if you see a generic type in an interface.



          Without generics:



          ArrayList list = new ArrayList();


          So, it would make a difference if you write:



          ArrayList</*Any type here*/> list = new ArrayList<>();


          Now the compiler has the information what type of objects are in list.



          But this one makes no real difference to the version without generics:



          ArrayList list = new ArrayList</*Any type here*/>();


          The variable list has no generics information accompanied, so it is as good (or bad) as the version without generics.






          share|improve this answer












          Generics are only present at compile time, they will be removed by the compiler (this is called type erasure). They are there for giving some type information to the compiler. This helps you avoiding type casts (as usual before Java 1.5) and allows the compiler for more type checking. It is also a valuable information to the programmer, for example if you see a generic type in an interface.



          Without generics:



          ArrayList list = new ArrayList();


          So, it would make a difference if you write:



          ArrayList</*Any type here*/> list = new ArrayList<>();


          Now the compiler has the information what type of objects are in list.



          But this one makes no real difference to the version without generics:



          ArrayList list = new ArrayList</*Any type here*/>();


          The variable list has no generics information accompanied, so it is as good (or bad) as the version without generics.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 14:52









          Donat

          48226




          48226












          • Yea, i get the point here.@Donat In fact, the java compiler should provide a type cast as described in the type erasure section on Oracle documentation. "Insert type casts if necessary to preserve type safety" source : docs.oracle.com/javase/tutorial/java/generics/erasure.html So, it means that just the reference/variable "list" could graft on this kind of type cast from the compilator? So, in this case: 'ArrayList list = new ArrayList</*Any type here*/>();' An instance, on the other hand could not? because otherwise it should provide a typesafe on the object pointed
            – DaviLevi
            Nov 10 at 16:00












          • Yes, the generic type information is attached to the variable and is there only at compile time. The instance exists at runtime and then the type infrmation has been removed.
            – Donat
            Nov 10 at 16:07


















          • Yea, i get the point here.@Donat In fact, the java compiler should provide a type cast as described in the type erasure section on Oracle documentation. "Insert type casts if necessary to preserve type safety" source : docs.oracle.com/javase/tutorial/java/generics/erasure.html So, it means that just the reference/variable "list" could graft on this kind of type cast from the compilator? So, in this case: 'ArrayList list = new ArrayList</*Any type here*/>();' An instance, on the other hand could not? because otherwise it should provide a typesafe on the object pointed
            – DaviLevi
            Nov 10 at 16:00












          • Yes, the generic type information is attached to the variable and is there only at compile time. The instance exists at runtime and then the type infrmation has been removed.
            – Donat
            Nov 10 at 16:07
















          Yea, i get the point here.@Donat In fact, the java compiler should provide a type cast as described in the type erasure section on Oracle documentation. "Insert type casts if necessary to preserve type safety" source : docs.oracle.com/javase/tutorial/java/generics/erasure.html So, it means that just the reference/variable "list" could graft on this kind of type cast from the compilator? So, in this case: 'ArrayList list = new ArrayList</*Any type here*/>();' An instance, on the other hand could not? because otherwise it should provide a typesafe on the object pointed
          – DaviLevi
          Nov 10 at 16:00






          Yea, i get the point here.@Donat In fact, the java compiler should provide a type cast as described in the type erasure section on Oracle documentation. "Insert type casts if necessary to preserve type safety" source : docs.oracle.com/javase/tutorial/java/generics/erasure.html So, it means that just the reference/variable "list" could graft on this kind of type cast from the compilator? So, in this case: 'ArrayList list = new ArrayList</*Any type here*/>();' An instance, on the other hand could not? because otherwise it should provide a typesafe on the object pointed
          – DaviLevi
          Nov 10 at 16:00














          Yes, the generic type information is attached to the variable and is there only at compile time. The instance exists at runtime and then the type infrmation has been removed.
          – Donat
          Nov 10 at 16:07




          Yes, the generic type information is attached to the variable and is there only at compile time. The instance exists at runtime and then the type infrmation has been removed.
          – Donat
          Nov 10 at 16:07


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234142%2fdifference-between-referencing-a-parameterized-type-instance-to-its-raw-type-and%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          how to define a CAPL function taking a sysvar argument

          Schultheiß

          Ansible :Unable to parse /etc/ansible/hosts as an inventory source