• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Assert doubt

 
Ranch Hand
Posts: 78
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Software Developer's Journal (01/2010)
Which assertion will compile AND will be done correctly:

d) boolean t;
assert t = false;
e) assert false : true;

I've just put 2 points which in the answer are CORRECT. In my view their're wrong, because we should assert that something is TRUE. Am I right?
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
assert statements come in 2 flavor.
the syntax of assert statement is :
assert (condition);

or

assert (condition): (xxxx);


here condition should be condition that will produce either true or false and (xxxx) can be anything whose string value is printed along with stack trace when assertion condition goes false.

so for first case : assert t = false; means assert false. remember an assignment statement returns the value being assigned , and in this case it returns false. so assert false; is a correct assert statement syntaxically.

the second case : assert false: true; here the assert false is sure to throw an assertion error. the second statement is 'true' which is a boolean literal and its string value is true so true is printed along with stack trace.remember you can use those expressions after the colon : that you can use with System.out.print( ) , that is which returns a string value.
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic