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

Assertion program

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone tell me how the following program works ..?
----------------------------------------------------------------------
public class TernaryAssertion{
public static void assertBounds(int low,int high,int value){
assert (value > low ? value < high : false)
: ( value < high ? "too low" :"too high");
}

public static void main(String[]args){
assertBounds(100,200,150);
}
}
-----------------------------------------------------------------------
[ February 28, 2006: Message edited by: Aafreen Moinuddin ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be rewritten with more brackets, so people don't have to remember, or consult documentation for, the operator precedence.

Also, I don't think that such a method is a very good idea. Assertion code is supposed to get stripped when running in non-assert mode. However, in your code, the call to the method will still occur, even in non-assert mode. The body of the method won't do anything, but the call will still happen.

So, a further re-think would be appropriate, to make the non-assert behaviour completely bypass all assertion code.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you understand how assert works? Do you understand the "?:" operator?

By the way, the assert would better be written as



or even better



Does the same, but communicates much better.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic