wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Self Test Chap 5 Question 4 - arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Self Test Chap 5 Question 4 - arguments" Watch "K&B Self Test Chap 5 Question 4 - arguments" New topic
Author

K&B Self Test Chap 5 Question 4 - arguments

Andreas Auer
Greenhorn

Joined: Jul 18, 2008
Posts: 4
Hi guys,

can someone please explain why there is/should be a different output using:

$java Input
and
$java Input 0

Why is the 2nd argument considered? I can only see 'args[0]' in code.
(As I don't know if I'm allowed to copy the whole question, I'll skip this until someone tells me anything different.

Thanks in Advance,
Andreas
_____________
Update:

class Input {
public static void main(String[] args) {
String s = "-";
try {
doMath(args[0]);
s += "t "; // line 6
}
finally { System.out.println(s += "f "); }
}

public static void doMath(String a) {
int y = 7 / Integer.parseInt(a);
}
}

command-line invocations:
java Input
java Input 0

Answer is "Each invocation produces a different exception."

Why???
[ July 18, 2008: Message edited by: Andreas Auer ]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

$java Input
and
$java Input 0

Why is the 2nd argument considered? I can only see 'args[0]' in code.


You can see the 'args[0]' , that means you are displaying the first command line argument !
What you want to convey us ? Pl elaborate it .

Post your code here .
[ July 18, 2008: Message edited by: Sagar Rohankar ]

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Originally posted by Andreas Auer:
Hi guys,

can someone please explain why there is/should be a different output using:

$java Input
and
$java Input 0

Why is the 2nd argument considered? I can only see 'args[0]' in code.
(As I don't know if I'm allowed to copy the whole question, I'll skip this until someone tells me anything different.

Thanks in Advance,
Andreas
_____________
Update:

class Input {
public static void main(String[] args) {
String s = "-";
try {
doMath(args[0]);
s += "t "; // line 6
}
finally { System.out.println(s += "f "); }
}

public static void doMath(String a) {
int y = 7 / Integer.parseInt(a);
}
}

command-line invocations:
java Input
java Input 0

Answer is "Each invocation produces a different exception."

Why???

[ July 18, 2008: Message edited by: Andreas Auer ]


Hello,

The 1st one will try to use the args[0] but it does not exists. So you'll get an ArrayIndexOutOfBoundsException.

In the 2nd case you'll get an ArithmeticException saying divison by 0 is not permited.

Raphael Rabadan.


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Self Test Chap 5 Question 4 - arguments