java - Tricking the JVM into casting arrays -
i'd know there way cast array of supertype
, array of subtype
. so:
subtype[] subarray = (subtype[])somesupertypearray;
i know @ runtime throws classcastexception
. way convert array copy array. in case, copying not option.
can trick jvm beliving cast valid, , bypass class type checks? creating reference array of supertype
, through seen subtype
array. (possibly facing dangers of unexplainable errors)
if define array like
supertype[] array = {element1, element2}; subtype[] array2 = (subtype[]) array;
it not work, can this:
supertype[] array = new subtype[]{element1, element2}; subtype[] array2 = (subtype[]) array;
you have make sure field array
has array of subtype when you're casting it.
Comments
Post a Comment