• 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

Assertions in Java 1.4

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the main purpose of assertions ?

Is just a small advantage to if statements ?

That's all ?

I am a bit confused with the addition of this feature adding a very little functionality
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.

Each assertion contains a boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw an error. By verifying that the boolean expression is indeed true, the assertion confirms your assumptions about the behavior of your program, increasing your confidence that the program is free of errors.

(reference- java.sun website)
http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assertions mainly help in testing/debugging your code. Its always nice to test your code with all your assumptions in it. It is usually disabled(which is of-course by default) during deployment.

Cheers,
Kits
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An assertion is an expression that evaluates TRUE, every time, at a fixed point on the code.

eg.

for( i = 0; i < 10; i++ ) {
// i >= 0 && i <= 9 is always true here

}

so, you could use assert to certify this:

for( i = 0; i < 10; i++ ) {
assert (i >= 0 && i <= 9);


}

When assertions is enabled, the expression is evaluated, and if the result is false, will throw an exception.

This is a tool to debug the code, and in production code, the assertions should be disabled, due to its overhead.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic