This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sierra/Bates question re: reassigning arrays" Watch "Sierra/Bates question re: reassigning arrays" New topic
Author

Sierra/Bates question re: reassigning arrays

Julian Reed
Greenhorn

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!
Julian
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
Greenhorn

Joined: Feb 12, 2003
Posts: 11
Thank you very much for this, that clear now!
Julian
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sierra/Bates question re: reassigning arrays
 
Similar Threads
Assigning array to existing array reference.
Array
confused
Array Creation
Arrays