• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Self Test Question

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer given to Question 4. at the end of chapter 4 doesn't seem to make a great deal of sense!

The answer according to the book is that because a non-short-circuit 'or' is used, both operands are evaluated and an ArrayOutOfBounds Exception is thrown due to referencing an element which is outside the variable's (args) length!

I compiled and ran the following code:


class Animal {}

public class Dog extends Animal {

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]);
}

}
}

using the following CLAs: Fork Live2

the result of which was the following output: ProductionFork


Can someone please clarify if I am missing something or whether there is indeed an error in this quetion?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the command line you use?
java Dog Fork Live2 //Then no exception is thrown
or
java Dog Live2 //the exception is thrown
 
Mark Brownengland
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been doing alot of shell programing recently and accidentally believed at the point of tackling this question that with the following command:

java Fork live2

arg[0] = "Fork" and arg[1] = "live2". Java does not include the class name in the args array so obviously args array contains only 1 element, thus causing an ArrayOutOfBounds Exception!
reply
    Bookmark Topic Watch Topic
  • New Topic