File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sierra/Bates question re: reassigning arrays 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 "Sierra/Bates question re: reassigning arrays" Watch "Sierra/Bates question re: reassigning arrays" New topic

Sierra/Bates question re: reassigning arrays

Julian Reed

Joined: Feb 12, 2003
Posts: 11
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!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.

SCJP2. Please Indent your code using UBB Code
Julian Reed

Joined: Feb 12, 2003
Posts: 11
Thank you very much for this, that clear now!
I agree. Here's the link:
subject: Sierra/Bates question re: reassigning arrays
It's not a secret anymore!