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 6900861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:36:01+00:00 2026-05-27T07:36:01+00:00

This works: Object[] array = new Object[3]; array[0] = ddd; array[1] = new Integer(12);

  • 0

This works:

Object[] array = new Object[3];  
array[0] = "ddd";  
array[1] = new Integer(12);  

This doesn’t: (crashes at new Integer)

Object[] array2 = new String[3];  
array2[0] = "ddd";  
array2[1] = new Integer(12);  

I’ve read about covariance but still can’t understand the underlying technical reason the second code example is prohibited, or why an ArrayStoreException is thrown.
What is essentially the difference between an array of Object references and an array of String references?

I understand that in the second example, the array gets instantiated with the intention of adding strings to it, but still, something doesn’t click logically. Can somebody explain it in simple terms?

  • 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-27T07:36:02+00:00Added an answer on May 27, 2026 at 7:36 am

    Let me make a small change to your second example to illustrate what this is really about:

    String[] array1 = new String[3];
    Object[] array2 = array1;
    array2[0] = "ddd";  
    array2[1] = new Integer(12);  // this one
    

    Lets assume that the flagged statement worked. (It doesn’t … but lets assume …)

    Now what happens if I do this?

    String wot = array1[1];  
    

    That assignment statement should be statically typesafe because array1 is declared as a String array. But it certainly IS NOT dynamically typesafe! In short, we’ve just broken Java static typing in a pretty fundamental way.

    Java avoids this breakage is by doing a runtime type check as part of the flagged statement, and not allowing a non-String to be assigned into an element of the String[] object.

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

Sidebar

Related Questions

This code works fine: $result = $client->__call(optionalInfo, array( new SoapParam(..., client), new SoapParam(..., add_code))
This works fine: var expectedType = typeof(string); object value = ...; if (value.GetType().IsAssignableFrom(expectedType)) {
This works just fine: protected void txtTest_Load(object sender, EventArgs e) { if (sender is
This works, but is it the proper way to do it??? I have a
This works: Tweener.addTween( [ _fee, _fye, _fum ] , { alpna:1, time:10 }); But
This works: #include <iostream> using namespace std; but this fails: #include <stdio> When is
This works : alert(document.getElementById(Container).nodeName); But this doesnt : var CurParent = document.getElementById(Container); alert(CurParent.nodeName); I
This works fine: XDocument xdoc = new XDocument( new XDeclaration(1.1, UTF-8, yes), new XProcessingInstruction(foo,
On my 64-bit machine, this C# code works: new byte[2L * 1024 * 1024
This works: <?php //compare1.php //**CLASS AND OBJECT class Entry { private $s_ids; public function

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.