Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

This is from Javaprepare.com

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one explain me about wrapper object. Thanks in advance.
 
reply
    Bookmark Topic Watch Topic
  • New Topic