• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sierra/Bates question re: reassigning arrays

 
Julian Reed
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you very much for this, that clear now!
Julian
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic