• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Sierra/Bates question re: reassigning arrays

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question reads:

and the command line invocation is:
java CommandArgsThree 1 2 3
what is the result?
A. 0 0
B. 1 2
C. 0 0 0
D. 1 2 3
E. Compilation fails
F. An exception is thrown at runtime
I got the question wrong since I though that
argCopy[0] needed to be an array of length 2. Anyway the answer is D, given that the reference variable argCopy[0] is reassigned to an array with three elements rather than two.
I'm still a bit confused about how this works though.
Thanks for any help!
Julian
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JLS 10.2


Because an array's length is not part of its type, a single variable of array type may contain references to arrays of different lengths.


Because "argCopy[0]" is a unidimensional array of string objects, "argCopy[0]=args;" works out. Now the length of the new array pointed by argCopy[0] is found out and its content is printed.
 
Julian Reed
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for this, that clear now!
Julian
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic