Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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

Integer??

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic