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?
java generics type-erasure raw-types
add a comment |
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?
java generics type-erasure raw-types
add a comment |
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?
java generics type-erasure raw-types
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
java generics type-erasure raw-types
edited Nov 9 at 22:41
rgettman
147k21201286
147k21201286
asked Nov 9 at 22:36
DaviLevi
82
82
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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