package localevidence;//: generics/unboundedwildcards1.java//: generics/ covariantarrays.javaclass fruit {}class appleee extends fruit {}class Jonathan extends appleee {}class orange extends fruit {}public class testwildcats {public static void main (String[] args) {Fruit[] fruit = new appleee[10];fruit[0] = new appleee (); // okfruit[1] = new jonathan (); // ok// runtime type is appleee[], not Fruit[] or orange[]:try {// compiler allows you to add fruit: Fruit[0] = new fruit (); // arraystoreexception} catch (Exception e) { system.out.println (e); }try {// compiler allows you to add Oranges:fruit[0] = new orange (); // aRraystoreexception} catch (exception e) { system.out.println (e); }}} /* output:java.lang.arraystoreexception: fruitjava.lang.arraystoreexception: orange*///:~
fruit[] Fruit = new appleee[10];//This is an instantiated Fruit array for appleee type
But compile without errors, but in runtime when the error?
This makes sense to the compiler, because it have a fruit[] reference-why shouldn ' t it allow a Fru It
Object, or anything descended from Fruit, such as Orange, to is placed into The array? So at compile Time , this is allowed.
Because this is reasonable for the browser, because
fruit[] Fruit = new APPLEEE[10];
Compiler allows to add fruit:fruit[0] = new Fruit (); Arraystoreexception
It has a fruit[] type of declaration, why can't you assign the fruit variable to it, or inherit fruit subclass? The compiler can see that there is no problem.
< Span style= "FONT-SIZE:11PT;" > the Runtime array mechanism, however, knows that it's dealing with An Apple [] and throws an exception when a foreign type was placed into the array.
< Span style= "FONT-SIZE:11PT;" > But the runtime mechanism thinks she's dealing with the appleee[] type, So if you put other types, you will get an error.
to another.
< Span style= "FONT-SIZE:11PT;" > "upward transformation" is actually not inappropriate, what you're actually doing is assigning an array of one type to another.
the array behavior is so it holds other objects, but because we were able to
upcast, it's clear that the array objects can preserve the rules about the type of objects they
contain. It's as if the arrays is conscious of what they is holding, so between the compile time checks and the runtime checks, Y ou can ' t abuse them.
this arrangement for arrays are not so terrible, because you do find out at run time, you ' ve
detection to compile time . So why happens when we try to use generic containers instead of
arrays?
The real issue is the talking of the type of the container, rather than the type.
The container is holding.
The type ofFlist isNowlist<, extends fruit>, which can read as "a list of any type
That ' s inherited fromFruit. "This doesn ' t actually mean theListWould hold any type of
Fruit however. The wildcard refers to a definite type, so it means "some specific type which
the flist List that ' s assigned have to IS Holding some
specified type such as Fruit /strong>or Apple Upcast to flist actually care."
package localevidence;public class holder<t> { private t value; Public holder () {} public holder (T val) { value = val; } public void set (T val) { value = val; } public T Get () { return value; } public boolean equals (Object obj) { Return value.equals (obj); } public static void main (String[] args) { Holder<Applee> Applee = new Holder<Applee> (New applee ()); Applee d = applee.get ();// applee.set (d); // holder<fruit> fruit = applee; // cannot upcast holder<? extends fruit> fruit = applee; // ok fruit p = fruit.get (); d = (Applee) Fruit.get (); // returns ' Object ' try { Orange c = (Orange) fruit.get (); // no Warning } catch (exception e) { system.out.println (e); } // fruit.set ( New applee ()); // cannot call set () // fruit.set (New Fruit ()); // cannot call set () system.out.println (Fruit.equals (d)); // ok }} /* Output: (Sample) java.lang.classcastexception: applee cannot be cast to orangetrue*///:
//: generics/supertypewildcards.javaimport java.util.*;p Ublic class supertypewildcards {static void writeto (List<? super apple> apples) {apples.add (New apple ()); Apples.add (New Jonathan ());// apples.add (New fruit ()); // error}} ///:~
< Span style= "FONT-SIZE:11PT;" >
Use-upcast for Java wildcard characters