Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here "Java Fork Live2" -->java Fork is to run Class and Live2 is the command line argument.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're thinking like a C programmer, not a java programmer.
 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stupid me, i get it now, thank you!
 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
one correction:
the answer is runtime exception, not compilation fails.

Alexsandra
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic