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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:38:28+00:00 2026-06-06T17:38:28+00:00

Why does my attempt to add an Integer to a Java array, declared as

  • 0

Why does my attempt to add an Integer to a Java array, declared as an Object[] but instantiated as a String[], not generate a compilation error?

Object[] ob = new String[1];
ob[0] = new Integer(1); // this shouldn’t compile but it does!

When I run this, I get a runtime exception instead of a (much preferred) compile-time error! Is this correct behavior? Shouldn’t I get a compile-time error?

  • 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-06-06T17:38:33+00:00Added an answer on June 6, 2026 at 5:38 pm

    It’s a choice that Java designers have made: String[] extends Object[], but you can’t add anything other than a String to an array whose concrete type is String[] without getting a runtime exception.

    What they could have made invalid is the following:

    Object[] ob = new String[1];
    

    because it effectively allows any kind of object to be added to the array without getting any compiler error (as you noticed), which is perfectly normal since an Integer is an Object, and the compile-time type of the array is Object[].

    They didn’t make this choice for arrays, but they did it for generic collections:

    List<Object> ob = new ArrayList<String>();
    

    generates a compiler error, because List<String> doesn’t extend List<Object>. Collections should generally be preferred over arrays, as they’re safer, and provide a lot more functionality.

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

Sidebar

Related Questions

I am working with a Python object that implements __add__ , but does not
Why does this attempt at creating a list of curried functions not work? def
Does anyone know if there is a way to generate different code in the
When I attempt to BuildSessionFactory() I get an error saying it cannot find the
Hi i'm trying to add a relative view to a merge adapter but it's
I am getting an exception: java.lang.IllegalArgumentException: cannot add to layout: constraints must be a
I need to get the password from a failed login attempt in WordPress but
Embedded Chromium does not have the same protection as stand-alone Chrome does when it
I have a simple problem: I add an object to an NSArray, then I
The context: We’re a small company that does not have an Exchange Server (or

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.