aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes This is from Javaprepare.com 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 "This is from Javaprepare.com" Watch "This is from Javaprepare.com" New topic
Author

This is from Javaprepare.com

Vijayalakshmi Chipada
Greenhorn

Joined: Dec 04, 2000
Posts: 17
What gets printed when the following code is compiled and run with the following command -
java test 2
Select the one correct answer.

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

A. test
B. test -1
C. 0
D. 1
E. 2
When I am compiling I am getting 1 Can you please explain this.
Thanks
------------------
Ch. Vijayalakshmi


Ch. Vijayalakshmi
vadiraj vd
Ranch Hand

Joined: Dec 15, 2000
Posts: 68


CODE
----
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) // line 1.
System.out.println(i);
if(args.length > 0) // line 2.
System.out.println(i - 1);
else
System.out.println(i - 2);
}
}

A. test
B. test -1
C. 0
D. 1
E. 2
-----------
END OF CODE

Hi Vijayalakshmi,
The command for execution is
java test 2
So only one comand line argument.
The length of command-line argument array is now 1.
That's why the test at line 1. fails(args.length > 1)
The length is not greater than 1 but equal to 1.
For the same reason the test at line 2. succeeds (args.length > 0)
So the output 1.
Hope this helps.

------------------
Regards
---------
vadiraj

*****************
There's a lot of I in J.
*****************


Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
Vijayalakshmi Chipada
Greenhorn

Joined: Dec 04, 2000
Posts: 17
Hi Vadiraj,
Thank you very much for your explanation, I had been confused with Wrapper obj of Integer.
Regards,


------------------
Ch. Vijayalakshmi
G Subrahmanyam
Greenhorn

Joined: Dec 16, 2000
Posts: 11
Can any one explain me about wrapper object. Thanks in advance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: This is from Javaprepare.com