The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operators (sourece k & b) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operators (sourece k & b)" Watch "operators (sourece k & b)" New topic

operators (sourece k & b)

krithika naya

Joined: Oct 25, 2007
Posts: 10
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

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
krithika naya

Joined: Oct 25, 2007
Posts: 10
Thank you very much.It is clear now.
I agree. Here's the link:
subject: operators (sourece k & b)
It's not a secret anymore!