In Java 1.5 what are Assertions ?

#2
Re: In Java 1.5 what are assertions?

q Assertions give you a way to test your assumptions during development and
debugging.
q Assertions are typically enabled during testing but disabled during deployment.
q You can use assert as a keyword (as of version 1.4) or an identifier, but not
both together. To compile older code that uses assert as an identifier
(for example, a method name), use the -source 1.3 command-line flag
to javac.
q Assertions are disabled at runtime by default. To enable them, use a command-
line flag -ea or -enableassertions.
q Selectively disable assertions by using the -da or -disableassertions flag.
q If you enable or disable assertions using the flag without any arguments,
you're enabling or disabling assertions in general. You can combine enabling
and disabling switches to have assertions enabled for some classes and/or
packages, but not others.
q You can enable and disable assertions on a class-by-class basis, using the following
syntax:
java -ea -da:MyClass TestClass
q You can enable and disable assertions on a package-by-package basis, and any
package you specify also includes any subpackages (packages further down the
directory hierarchy).
q Do not use assertions to validate arguments to public methods.
q Do not use assert expressions that cause side effects. Assertions aren't guaranteed
to always run, and you don't want behavior that changes depending
on whether assertions are enabled.
q Do use assertions—even in public methods—to validate that a particular
code block will never be reached. You can use assert false; for code that
should never be reached, so that an assertion error is thrown immediately if
the assert statement is executed.
 
#4
Hi,

Assertation is an error handling technique, using this we can check our assumptions on development and debugging time. Means that you know 100% input data will be come. Using this we can check the integrity of our functionality. On development and debugging time we can enable Assertions using [-ea or -enableassertions].

Example : java -ea TestAssertionClass

Also when ever we want to deploy application that time we can disable Assertions using [-da or -disableassertions].
Example : java -da TestAssertionClass

Practical Example :
Code:
//Old Technique without assertation
private void methodA(int num) {
    useNum(num + x); // we've tested this;
    // we now know we're good here
}

//New Technique with assertation
private void methodA(int num) {
    assert (num>=0); // throws an AssertionError
                            // if this test isn't true
    useNum(num + x);
}
 
#5
in simple words one can say assertions are used to handle user defined exceptionz that a user expects to occur but jvm cant find out thuz errors...
let me give u an example....
lets take an example of the application where jvm is assigned to calculate the pension of the customer...in such case jvm will calculate the pension of the customer whuz age cud be 700years too...coz jvm just chekz out if integer is given for age or not but not the value...hence to avoid such usesr defined buisness errors.we use sumthing called as Assertions, previously these user defined exceptions were handled using "throw" keyword....but jdk 1.5 has introduced sumthing called as assertions which made developer's job mur easy......
HOPE am clear:)
 
Top