• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Generic Arrays & SCJP

Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody:

Does objective 6.4 of the exam cover generic arrays? I am always confused as to how detailed the exam questions will be.

I am using the Bates/Sierra study guide, and they don't cover some of the nuances of generics as in depth as the Sun Java tutorial: Generics Tutorial

Here are some code snippets:

As a note: I believe the third example is wrong. When I try this code out myself, I get a compiler error, not a compiler warning.
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Straight from AngelikaLanger's Generics FAQ's:

Can I create an array whose component type is a wildcard parameterized type?
No, because it is not type-safe.
The rationale is the same as for concrete parameterized types: a wildcard parameterized type,
unless it is an unbounded wildcard parameterized type, is a non-reifiable type and arrays of
non-reifiable types are not type-safe.
The array store check cannot be performed reliably because a wildcard parameterized type
that is not an unbounded wildcard parameterized type has a non-exact runtime type.
Example (of the consequences):
Object[] numPairArr = new Pair<? extends Number,? extends
Number>[10]; // illegal
numPairArr[0] = new Pair<Long,Long>(0L,0L); // fine
numPairArr[0] = new Pair<String,String>("",""); // should fail,
but would succeed
The array store check would have to check whether the pair added to the array is of type
Pair<? extends Number,? extends Number> or of a subtype thereof. Obviously, a
Pair<String,String> is not of a matching type and should be rejected with an
ArrayStoreException. But the array store check does not detect any type mismatch,
because the JVM can only check the array's runtime component type, which is Pair[] after
type erasure, against the element's runtime type, which is Pair after type erasure.

You should read this:


This tiny ad is wafer thin:
Garden Master Course kickstarter
    Bookmark Topic Watch Topic
  • New Topic