hi, if i try to compile the following code by "java temp56 2" , i get 1 as result. Why does 2 not show up due to line marked **. public class test { public static void main(String args[]) { Integer intObj=Integer.valueOf(args[args.length-1]); int i = intObj.intValue(); if(args.length > 1) System.out.println(i); ** if(args.length > 0) System.out.println(i - 1); else System.out.println(i - 2); } }
Ahmad Mudassir
Greenhorn
Joined: Oct 16, 2000
Posts: 10
posted
0
i wonder how you compiled test class by java temp56. any way if you are getting 1 as a output its right. As comes into the check if args>0 and 2-1 is 1 Ahmad
Raghvendra Sharma
Ranch Hand
Joined: Oct 09, 2000
Posts: 82
posted
0
Dear You couldn't have compiled this code with the class name as test and the compilation statement referring to some other class name. Any way everything is fine since the length of the args array is 1 and is not greater than 1 ( args.lenght > 1 is false). But it is greater than 0 so the result you get is i-1 i.e 2-1. Hope it helps. raghav..
Sagar Sharma
Ranch Hand
Joined: Aug 31, 2000
Posts: 92
posted
0
hi, i am sorry for the typing error, the class name should be temp56. but at line 3 see what happens: 1) args.length=1 so args.length-1 = 0 2) now args[0] = 2 3) so the value got by intObj is "2" 4) now the value for i at line 4 becomes 2 and not 1.
I am confused ...can someone help me out please
Sagar
deekasha gunwant
Ranch Hand
Joined: May 06, 2000
Posts: 396
posted
0
Hi sagar, although i is equal to 2 . The line marked by ** never gets executed since it will be executed only if (args.length > 1) and as u've urself mentioned u've got length =1 not >1. hope it is clear. regards deekasha