Why does Java Arrays allow us to initialize value to an index which is beyond the size of the array?
-
int[] intArray = new int[4]; If we try to do the following why can't the compiler say that we are trying to assign something beyond the array upper index. intArray[5] = 50; Why did Java developers decided not to show this as an error? If possible please answer this as well
-
Answer:
The length of an array and the index used to access it are often not known at compile time. Determining the length and index is in general undecidable. Even in cases where it is decidable, it's not free. It would make compilation take longer. Basically it's not a valuable enough feature to be worth the cost.
Tim Wilson at Quora Visit the source
Other answers
Most likely: it's a corner case that nobody thought was important or common enough to bother writing additional code specifically to check for, and the runtime error this condition generates in the simple cases you'd be able to detect at compile time is already good enough to allow the bug to be found trivially. Making it work reliably at compile time for anything other than very simple cases requires data flow analysis that gets hairy fast. For example, you could have something like int[] intArray = new int[4]; if (someCondition) { intArray = new int[5]; } if (someOtherCondition) { intArray[4] = 1234; } and, while it's not really a good idea to do something like this, it might or might not be erroneous depending on the surrounding logic, something that's far outside the purview of the kinds of analysis done by a typical compiler. The Java compiler, in fact, does very little static analysis of code, either for correctness or for performance. It relies on the JVM to do optimization based on runtime profiling data, and its design assumes (correctly, in my view) that any developer who wants extensive code analysis will use one of the other development tools designed for that purpose. There are plenty of automated bug-detection tools to choose from: CodeSonar, PMD, and FindBugs, to name three. With a few exceptions, adding static analysis to attempt to spot logic bugs will make a compiler slower or more memory-intensive or both. If the analysis is built into the compiler, you will pay that price on every edit-compile-test cycle, even when you're making cosmetic changes that have near-zero chance of introducing bugs. There is always a tradeoff between speed and functionality; the Java compiler team has decided to err on the side of speed and leave those pieces of functionality to other tools.
Steven Grimm
Its probably not worth checking for it as anyways it will throw an Exception at runtime. Moreover in real life , both the exact length of an array and the index used to access it are rarely known at compile time.
Kaustubh Saha
The array is constructed at runtime so its actual siz is not known at compile time where initialization occuers.
Walid Aly
Related Q & A:
- What Is The Name Of That Which Exists Beyond The Universe?Best solution by Astronomy
- How to find the index of value in python list?Best solution by Stack Overflow
- How to send a file which is bigger in size through e-mail?Best solution by Yahoo! Answers
- Why are most of the US to India flights flown via london why not via Australia?Best solution by Quora
- Why do flight attendants ask us to open the window shades before take off and landing?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.