wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array size must be known at compile time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array size must be known at compile time?" Watch "Array size must be known at compile time?" New topic
Author

Array size must be known at compile time?

Mateusz Papiernik
Greenhorn

Joined: Jul 05, 2009
Posts: 13
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


SCJP 1.6
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Look at that:

Runs well. The size of array can be declared during runtime. JVM allocates the memory dynamically.


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Mateusz Papiernik
Greenhorn

Joined: Jul 05, 2009
Posts: 13
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jul 05, 2009
Posts: 13
Thanks for your explanation! I now understand the idea of the question well.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array size must be known at compile time?
 
Similar Threads
Marcus exam 3 # 26
Sun Cirtification
array assignments
Arrays
Very confused with "object behavior"