Arrays are redefined in Java

I recently ran into this, arrays are reanimated in Java. That is, they only know type information at runtime. But I'm a little confused by this definition.

If Arrays is said to know type information only at run time, I should literally be able to assign any values ​​to any arrays, since typing is known only at run time errors that will be thrown only at run time. But this is not so in real time. For this, we get a compile-time error.

So can someone shed light on "what does it mean when arrays are massaged"?

+6
source share
4 answers

I think this means that line of code data will throw an exception:

String[] arrayOfStrings = new String[10]; Object[] arrayOfObjects = arrayOfStrings; // compiles fine arrayOfObjects[0] = new Integer(2); // throws a runtime exception (ArrayStoreException IIRC) 

Arrays are covariant: String [] extends Object []. But the actual type of the array is known at runtime, and trying to save an instance that is not of the correct type raises an exception.

+7
source

I believe the term you are looking for is reifiable .

The restored type does not lose type information due to erasing styles at runtime. Examples of repeating types include:

  • primitives
  • not common link types
  • arrays or primitives or arrays of non-generic link types.

Confirmed does not mean that the type is not known at compile time. This means that something like the following cannot be typed checked:

 List<Integer>[] myList; 

Arrays carry information about the time that they store. Unsafe types cannot be checked at runtime, which does not make them good candidates for the type of an array component.

When using re-identifiable types as the type of an array component, such as String[] , complete type information is available at runtime, so type checks can be performed.

 String[] someArray = new String[2]; //some factory returns Integer upcasted to Object someArray[0] = someFactory.getType("Integer"); //throws RuntimeException 

Sources:

http://docs.oracle.com/javase/tutorial/java/generics/nonReifiableVarargsType.html http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#FAQ106 (Good)

+4
source

If Arrays is said to know type information only at run time, I should literally be able to assign any values ​​to any arrays, since typing is known only at run time errors that will be thrown only at run time. But this is not so in real time. For this, we get a compile-time error.

Reifiable types know their type at runtime and compile time, so the compiler will still prevent you from making silly errors wherever possible (what does it mean to skip them?)

However, sometimes when the compiler cannot always decide whether an assignment (for example) will be valid because it does not know the exact types, and this can check the reified types. For instance:

 Object[] arr = new String[5]; arr[0] = 7; 

... this compiles because in the second line the compiler knows only the static type of the array as Object , while the dynamic type is something more specific. In Runtime, it will crash as an exception, which can only be because (unlike the general collection classes) a certain type is known at runtime.

+3
source

As mentioned in the doc :

A type being acknowledged is a type whose type information is fully accessible at run time. This includes primitives, not generic types, raw types, and calls to unrelated wildcards.

Non-recoverable types are types in which information is deleted compiled by the erase type — calls to common types that are not defined as unlimited wildcards. A non-recoverable type does not have all of its information available at runtime. Examples of nonrepairable types are List and List; The JVM cannot tell the difference between these types at runtime. As shown in the section "Constraints on In General, there are certain situations where non-recoverable types cannot be used: in the case of an expression, for example, or as an element in an array.

So arrays are reified and covariant, but generics are invariant and erased by nature. Arrays provide runtime type security and throw an ArrayStore exception if an element of the correct type is not added.

+1
source

Source: https://habr.com/ru/post/944858/


All Articles