MY mENU


Saturday 26 January 2013

What does the assert keyword do in Java?


assert is a keyword added in Java version 1.4.
assert tests the programmer's assumption during development without writing exception handlers for an exception. Suppose you assumed that a number passed into a method will always be positive. While testing and debugging, you want to validate your assumption. Without the assert keyword, you will write a method like: 

private void method(int a) { 

if (a >= 0) {
// do something that depends on a not being negative
} else {
// tell the user that a is negative
}

This is simple exception handling; consider the case of big one. Assertion will come into the picture when you don't want to take the time to write the exception handling code.
Consider the above program with assertion:

private void method(int a) {
assert (a>=0); //throws an assertion error if a is negative.
// do stuff, knowing that a is not negative
}

In this program, assert (a>0) will pass when 'a' is only positive. Isn't this much cleaner than the previous example? If the value of 'a' is negative, then an AssertionError will be thrown.
There are two types of assertions: 
  1. Simple
  2. Really simple
Example of a really simple assertion:

private void method() {
assert (x>0);
//do more stuff
}

Example for simple assertion: 
private void method() {
assert (x>0) : "X is" + x ;
// do more stuff
}
The difference between these two is that the simple assertion - the second example - appends the expression after the colon to the error description in the stack trace.
Note: assertion code - in effect - evaporates when the program is deployed.

For assertion first you need to invoke the assertion.
Asserts are disabled by default in the JVM. In order to run a program with asserts you must use the -ea command line parameter (i.e. java -ea AssertTest).
 
Even though your compiler suggested it was using java version 1.4, it wasn't actually because you compiled using the "javac [files]" command instead of the "javac -source 1.4 [files]" command. Assertions only work in version 1.4 and later versions.
 
When you are using assert keyword, you have to enable it. Otherwise it wont give any result. 
java -enableassertions classfile

Final method and Final class


Final method: a method which has final keyword in its definition is called final method.

When a method should be defined as final?

If we do not want allow subclass to override super class method and to ensure that all sub classes uses the same super class method logic then that method should be declare as final method.

Final method cannot be overridden in subclass, violation leads to CE:

What is the difference between private and final methods?

Private method is not inherited, where as final method is inherited but cannot be overridden.

Can we overload final method in subclass?
Yes, we can overload final method in subclass.

Class Ex {
Final void m1 (int x) { }
}

Class Sa extends Ex{
Void m1(){}
}

Final Class:

A class which has final keyword in its definition is called final class.

When a class should be defined  as final?
In the below situations we must define class as final
  1. If we do not want define all methods as final
  2. If we do not want allow subclasses to extend our class functionality.

Sub class cannot be defined from final class, violation leads to CE:

Final class Ex{
}

Final class members are not final until they are declared as final members explicitly using final keyword.