wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about Array index - ojective 7.6 on K&B SCJP book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about Array index - ojective 7.6 on K&B SCJP book" Watch "question about Array index - ojective 7.6 on K&B SCJP book" New topic
Author

question about Array index - ojective 7.6 on K&B SCJP book

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
This is in chapter 4 page 309...
Given:
class Fork {
public static void main(String[] args) {
if(args.length == 1 | args[1].equals("test")) {
System.out.println("test case");
} else {
System.out.println("production " + args[0]);
}
}
}
And the command-line invocation:
java Fork live2

What is the result?

the answer is compilation fails. the book states "Because the short circuit (||) is not used, both operands are evaluated. Since args[1] is past the args array bounds, an ArrayIndexOutOfBoundsException is thrown"

I just dont get that..."Fork" on the command line invocation shall be args[1], and java, lives are args[0] and [2] respectively.

wat's the trick here any masters?
Arun Kumar Gaddam
Ranch Hand

Joined: May 05, 2007
Posts: 58
Here "Java Fork Live2" -->java Fork is to run Class and Live2 is the command line argument.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

You're thinking like a C programmer, not a java programmer.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
stupid me, i get it now, thank you!
Alexsandra Carvalho
Ranch Hand

Joined: Jul 13, 2007
Posts: 75
Hello,
one correction:
the answer is runtime exception, not compilation fails.

Alexsandra
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about Array index - ojective 7.6 on K&B SCJP book