How does java stores type indication? [on hold]
How does java stores type indication? [on hold]
While reading the book Java Generics and Collections (O'Reilly), my attention got cached by below paragraph
Another consequence of implementing generics by erasure is that array types differ in key ways from parameterized types. Executing new String[size] allocates an array, and stores in that array an indication that its components are of type String. In contrast, executing: new ArrayList() allocates a list, but does not store in the list any indication of the type of its elements. In the jargon, we say that Java reifies array component types but does not reify list element types (or other generic types).
Can anyone brief how java maintains the scenario in italic text?
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
You are asking about
erasure
. Many other questions have been asked about that topic in the past. This question is likely a duplicate of one or more of them.– David Conrad
Jun 30 at 3:22
erasure
2 Answers
2
Executing new String[size] allocates an array, and stores in that array an indication that its components are of type String
.
String
No it doesn't. It first creates an array class for the element type if necessary, and then instantiates it with the specified number of elements. In Java, any array is an object, and any object contains a reference to its own class, and the array class knows its own element type. It is not nearly as simple as 'stores in that array an indication that its components are of type String
'.
String
In contrast, executing new ArrayList()
allocates a list, but does not store in the list any indication of the type of its elements.
new ArrayList()
That's not correct either. ArrayList
is specified as having elements of type E
, where E
is unbounded, which means it is erased in the object code for ArrayList
to java.lang.Object
. What isn't stored is the actual type of E
for any given instantation.
ArrayList
E
E
ArrayList
java.lang.Object
E
It's just is what it says.
String arr=new String[5] means a String array of 5 String elements. You cannot compile code arr[3]=5 if arr is String array.
Whereas ArrayList arr = new ArrayList(); is just a pointer to some array that may contain anything.
Here is the scenario:
public class Main
{
public static void main(String args)
{
ArrayList arr = new ArrayList();
// I can add anything to my array
arr.add(5); // I can add an Integer to element 0
arr.add("Hello"); // I can add a String to element 1
arr.add(3.1415936); // I can add a Float to element 2
// and then, before using the array elements, I can test them.
// Example, I test element 0
if(arr.get(0) instanceof String){
System.out.println("Element 0 is String");
}
if(arr.get(0) instanceof Integer){
System.out.println("Element 0 is Integer");
}
}
}
It turns out, in the Runtime, Element 0 is Integer
.
Element 0 is Integer
You can test it. It works as I described.
That seems to be a poorly worded paragraph. Arrays and containers are totally different concepts. However, the question is borderline off-topic here. Please visit the help center and read How to Ask to learn how to use this site.
– Jim Garrison
Jun 30 at 3:12