• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Array size must be known at compile time?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 26697
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your explanation! I now understand the idea of the question well.
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic