Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

This is from Javaprepare.com

 
Vijayalakshmi Chipada
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vadiraj vd
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
*****************
 
Vijayalakshmi Chipada
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vadiraj,
Thank you very much for your explanation, I had been confused with Wrapper obj of Integer.
Regards,


------------------
Ch. Vijayalakshmi
 
G Subrahmanyam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one explain me about wrapper object. Thanks in advance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic