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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:03:46+00:00 2026-06-04T02:03:46+00:00

While fiddling around in Java, I initialized a new String array with a negative

  • 0

While fiddling around in Java, I initialized a new String array with a negative length.
i.e. –

String[] arr = new String[-1];

To my surprise, the compiler didn’t complain about it.
Googling didn’t bring up any relevant answers. Can anyone shed some light on this matter?

Many thanks!

  • 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-04T02:03:47+00:00Added an answer on June 4, 2026 at 2:03 am

    The reason is that the JLS allows this, and a compiler that flagged it as a compilation error would be rejecting valid Java code.

    It is specified in JLS 15.10.1. Here’s the relevant snippet:

    “… If the value of any DimExpr expression is less than zero, then a NegativeArraySizeException is thrown.”

    Now if the Java compiler flagged the code as an error, then that specified behaviour could not occur … in that specific code.

    Furthermore, there’s no text that I can find that “authorizes” the compiler to reject this in the “obvious mistake” cases involving compile-time constant expressions like -1. (And who is to say it really was a mistake?)


    The next question, of course, is ‘why does the JLS allow this?’

    You’ve need to ask the Java designers. However I can think of some (mostly) plausible reasons:

    • This was originally overlooked, and there’s no strong case for fixing it. (Noting that fixing it breaks source code compatibility.)

    • It was considered to be too unusual / edge case to be worth dealing with.

    • It would potentially cause problems for people writing source code generators. (Imagine, having to write code to evaluate compile-time constant expressions in order that you don’t generate non-compilable code. With the current JLS spec, you can simply generate the code with the “bad” size, and deal with the exception (or not) if the code ever gets executed.)

    • Maybe someone had a plan to add “unarrays” to Java 🙂


    Other answers have suggested that the compiler could / should “flag” this case. If “flagging” means outputting a warning message, that is certainly permitted by the JLS. However, it is debatable whether the compiler should do this. On the one hand, if the above code was written by mistake, then it would be useful to have that mistake flagged. On the other hand, if it was not a mistake (or the “mistake” was not relevant) then the warning would be noise, or worse. Either way, this is something that you would need to discuss with the maintainer(s) for the respective compiler(s).

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

Sidebar

Related Questions

I'm using Visual Studio and while fiddling around with a custom control, I started
While fiddling around with a custom look for the web interface for svn over
I have been fiddling around with settings for a while, and cannot seem to
While fiddling around with the settings on the device I'm testing my app on
While fiddling around to try to automate some process, I ran into this seemingly
So been fiddling around with this for a while now but still no luck.
I have this, and i've been fiddling around with it for a while but
Remember, this is using python. Well, I was fiddling around with an app I
In Java is there anyway to have one constructor that will accept an array
While fiddling with WCF fault contracts, I was hoping I could reuse my custom

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.