• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operators (sourece k & b)

 
krithika naya
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Fork {
public static void main(String[] args) {
if(args.length == 1 | args[1].equals("test")) { // line 1
System.out.println("test case");
} else {
System.out.println("production " + args[0]);
}
}
}
And the command-line invocation:
java Fork live2
What is the result?
A. test case
B. production
C. test case live2
D. Compilation fails.
E. An exception is thrown at runtime.

the answer given is E is correct. Because the short circuit (||) is not used, both operands are evaluated. Since
args[1] is past the args array bounds, an ArrayIndexOutOfBoundsException is thrown.

please explain line 1.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you invoke the main method with
java Fork live2

then you have an String array with the length of one and with only one element: "live2".
The length is one, so it has only the element with the number zero.
args[0] == "live2"
args[1] does not exist, therfore you get an ArrayIndexOutOfBoundsException.


Yours,
Bu.
 
krithika naya
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.It is clear now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic