File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array size must be known at compile time?

 
Mateusz Papiernik
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

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 in advance for clarifying this one for me!

Best regards,
Mateusz
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at that:

Runs well. The size of array can be declared during runtime. JVM allocates the memory dynamically.
 
Mateusz Papiernik
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mateusz Papiernik
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your explanation! I now understand the idea of the question well.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic