Sorry, I was obviously writing this at the same time as John...
Anyone please let me know the output of both...
Since you've supplied the output, I assume you actually don't mean that! As far as explanation goes:
In the first example, there are 4 command line arguments { "lets", "see", "what", "happens" }, but these are referenced on a 0-based index, so in fact although there are 4 entries, they are actually indexed by 0, 1, 2, 3. So in the println() argument we have:
args[0] which references "lets" as the first entry
args[args.length - 1] == args[4-1] == args[3] which references "happens"
So you see, the last index is the length of the array subtract 1.
In the second example, you attempt to access args[args.length], but this is
always out of range, because the last index is always (args.length - 1) and not args.length.
Using the previous command line data we would be trying to reference:
args[args.length] == args[4] which does not exist!
Therefore an ArrayIndexOutOfBounds is thrown. Does that make sense? If it's array indexing that troubling you, remember that arrays are 0-based, not 1-based as our counting usually is; we count 1, 2, 3..., arrays count 0, 1, 2... and so the last index is one less than the length.
[ April 16, 2006: Message edited by: Charles Lyons ]