• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Question on Assertions

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will be the value of variable "totalParams" after line 7;if the following code is executed from command line as - java -ea AssertTest?
(Assume that the code is compiled and run with assertions enabled)

1.public class AssertTest
2.{
3.public static void main(String args[])
4.{
5.int count = args.length;
6.assert (Math.abs(count) == count);
7.int totalParams = count;
8.//more code
9.}
10.}
The Ans for the above question is given
It will throw an AssertionError at runtime at line 6, and statements after that line are not executed;hence value of totalParams can not be determined
I think this is wrong, it will print 0 as output.
kindly correct me If i am wrong..
Thanks in advance.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran it in my machine with no problems. I don't know why it should throw assertion error.

I think this is wrong, it will print 0 as output.
I think is wrong too.. but why do you think it prints 0?
[ July 29, 2003: Message edited by: Andres Gonzalez ]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Majohnad,
The args.length is 0,if you are not providing any command line arguments. Here Math.abs(count) == count returns true.
If the expression in the assert statement is true it won't throw any assertion error.
assert(Math.abs(count) == count) becomes assert(true). The control goes to next line. It prints 0, If you have System.out.println(totalParams).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic