I've been doing mock exams from our forum friend at javabeat.net and found a question asking to point correct statements. One of them was:
The size of Java Arrays should be specified at the compile. Hence they are static arrays.
Which was marked as the correct answer in the answer sheet. How can be this true, assuming that...
...compiles fine and shows that array size may be specified at runtime? Or maybe I'm misunderstanding the question, and the point is whether ANY size must be supplied, as opposed to collections that can shrink/enlarge after initialization?
Thanks, I know - the sample I provided runs well too, and provides the size at runtime too. I wonder whether the question/answer in the mock exam is errornous, or I have misunderstood the sentence quoted.
The question doesn't have particularly good grammar, so perhaps it isn't surprising it can be misunderstood. It says "should" which means it's optional to specify the size but it's better to specify the size than not to. Really it should say "must", which means that it is required, not optional, to specify the size.
To your point, though, it doesn't say that the size of an array must be a constant specified at compile time, just that it must be specified at compile time. That does allow for the size of an array to be a variable, then.
Joined: Jul 05, 2009
Thanks for your explanation! I now understand the idea of the question well.
subject: Array size must be known at compile time?