• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arrays of specific generic types are not allowed.Explain.

 
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from The Complete Reference Java(7th ed) (Herbert Schildt),page 356:

You can't declare an array of references to specific generic type.
[B]That is, this line:
won't complile.Arrays of specific generic types simply are not allowed,because they can lead to a loss of type safety.[/B]
You can create an array of references to a generic type if you use a wildcard,however, as shown here:
This approach is better than using an array of raw types,because atleast some type checking will still be enforced.



Can anyone please explain this?

[ December 15, 2008: Message edited by: Daisy Lakhanpal ]
[ December 15, 2008: Message edited by: Daisy Lakhanpal ]
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it is not type-safe.
Arrays are covariant, which means that an array of supertype references is a supertype of an array of subtype references. That is, Object[] is a supertype of String[] and a string array can be accessed through a reference variable of type Object[].



When an element is inserted into the array, the information about
the array's component type is used to perform a type check - the so-called array store check. In our example the array store check will fail because we are trying to add a Long to an array of Strings. Failure of the array store check is reported by means of a ArrayStoreException.

But because of type erasure of generics, parameterized types donot have exact runtime type information. So there is no array-store-check possible in generics case.



Suppose Arrays of concrete parameterized type were allowed, then what would have happened?

Gen<Integer> gens[]=new Gen<Integer>[10];
Object[] objArray=gens;//line 1



We could assign this array to Object[] objArray at line1.

objArray[0]=new Gen<String>("Passing String inplace of Integer");////this should fail, but not as runtime there is not generic info



And we could have stored String Object in the array opposed to Integer Array. At runtime an array store check must be performed when an array element is added to the array. Since we are trying to add a Gens<String> to a Gens<Integer>[] we would expect that the type check fails.

However, the JVM cannot detect any type mismatch here: at runtime, after type erasure, objArray would have the dynamic type Gen[] and the element to be stored has the matching dynamic type Pair. Hence the store check succeeds, although it should not.

The array in our example would contain different types of Gens instead of pairs of the same type. This is in contradiction to the expectation that arrays hold elements of the same type (or subtypes thereof).

This undesired situation would most likely lead to program failure some time later, perhaps when a method is invoked on the array elements.
like:

Integer i=gens[0].getObj();



Now JVM would have executed above code that is actually returning String, so it would have tried to convert String to Integer and this would have cause java.lang.ClassCastException.

In order to prevent programs that are not type safe all arrays holding elements whose type is a concrete parameterized type are illegal.
[ December 15, 2008: Message edited by: Punit Singh ]
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation punit
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Punit,
Thats what i made out from your detailed explanation.

In Non Generic code::


If we use Generics like ::
line 2 would not give any run time exception as generic info is not there at run time. and line 1 will appear to JVM as and line 2 as ,so there will be no ArrayStoreException!
And it would add objects of Gen<Integer> to a an array of Gen<String>. But we expect only Gen<String> in this array.So to avoid this ,compiler does not allow declaration as in line 1 .

Am I right?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See also my blog post about this.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right Daisy!
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic