Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7691899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:43:43+00:00 2026-05-31T20:43:43+00:00

While answering to a question about that here: https://stackoverflow.com/a/9872630/82609 I tried to do the

  • 0

While answering to a question about that here: https://stackoverflow.com/a/9872630/82609

I tried to do the following:

Comparator<String>[] comparators = new Comparator[] {...};

It works! But the following doesn’t:

Comparator<String>[] comparators = new Comparator<String>[] {...};

On the related question, i made the assumption:

I guess it’s because initially the array contract may be something
like this:

If you create an array of type X, you will NEVER EVER be able to put
anything in it that IS-NOT-AN X. If you try, you’ll get an ArrayStoreException

Thus allowing arrays with generics creation would lead to a different
rule like:

If you create an array of type X<Y>, you will NEVER EVER be able to
put anything that IS-NOT-AN X. If you try, you’ll get an
ArrayStoreException. But you CAN add both X<Y> and X<Z> objects because of type erasure!


But thinking about it, would it really be a problem to have:

Comparator<String>[] comparators = new Comparator<String>[] {...};

I don’t really understand why it’s not possible, since using such a thing would:

  • Check the classes inserted at runtime
  • Check the classes type inserted at compile time

Finally we can use an array with generic type reference and because of the impossibility to create an array with a generic type, i think many people do not even know it’s possible.

I just wonder if someone knows the reason behind this choice?

It’s a bit like forcing people to use List<String> = new ArrayList(); instead of using List<String> = new ArrayList<String>();


dimitrisli you gave a nice exemple from Joshua Bloch’s famous book.
As you/he explained it, it is dangerous to use both generic arrays + covariance and could lead to ClassCastException while we expect ArrayStoreException from an array with use of covariance.

But please notice the following is still legal and lead to the same:

List<String>[] stringLists = new List[1];
List<Integer> intList = Arrays.asList(42);
Object[] objects = stringLists;
objects[0] = intList;
String s = stringLists[0].get(0);

However it produces an unchecked cast warning at compile time, and as you mentionned, a ClassCastException at runtime.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:43:44+00:00Added an answer on May 31, 2026 at 8:43 pm

    I see where you’re coming from (and in a practical sense I basically agree), but I think there is a difference that motivates the current situation.

    As you mention, erasure means that generic parameters aren’t available at runtime and so the types are checked at compile time (be that for a List<String> or your Comparator<String>[]). Critically, this is based on the generic parameter of the variable.

    Arrays on the other hand check the types of their argument at runtime, when they’re inserted, so they can throw an ArrayStoreException if they’re misused (typically due to abusing their covariance). Arrays therefore need to be able to perform both of your bullet point checks internally, and of course they can’t check the generic parameter at runtime. Hence it makes no sense to instantiate a generic array, since the array would have to completely ignore the generic parameter, which would be misleading at best.

    That said, it does make sense to assign such an array to a parameterised reference, since then the compiler can perform generic checks. And you’re right in thinking that this covers all the bases and ensures that the generic types are checked (so long as the variables are parameterised correctly).

    The bottom-line reason behind this choice, and why arrays are different to collections in this respect, is that arrays are required to actually check the types of their arguments when they’re inserted, whereas collections merely take your word for it and allow type errors to bubble through into a ClassCastException later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While answering this question I noticed that I got the following dialog while atempting
While answering another question, I thought of the following example: void *p; unsigned x
While answering a question on SO yesterday, I noticed that if an object is
While answering a particular question here in SO I stumbled upon a peculiar issue
While answering this question I noticed that I have never come across any property
While answering Static class variables in Python I noticed that PythonWin PyWin32 build 209.2
Before answering this question, understand that I am not asking how to create my
Somebody has posted an hour ago or so a question that was about the
While answering this question C# Regex Replace and * the point was raised as
While answering this question regarding safe escaping of filename with spaces (and potentially other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.