Hi Sweety,
1. i wanted to know if both the following declaration for enabling and disabling the assertions at runtime are correct.
java -ea com.geelcsanonymous.Foo and
java -ea:com.geeksanonymous.Foo
Yes, both lines above are correct, and you can use either of them to enable assertions at runtime.You can specify -ea / -ea: followed by a package name or a class name.
You can also selectively enable/disable assertions.
For eg:
java -ea -da:com.geeksanonymous.Foo
The above line means: enable assertions in general, but disable them for the class com.geeksanonymous.Foo
2. can anyone give me an example for not using assertions to validate command line arguments.
If you are using K&B, there is some nice explanation for this given under the heading "Don't Use Assertions to Validate Arguments to a Public Method".
3. i have not understood this:
code:
switch(x) {
case 2: y = 3;
case 3: y = 9;
case 4: y = 27;
default: assert false; // We're never supposed to get here!
}
If you have a switch statement with no default case, you are assuming that one of the cases will always be executed. To
test this assumption, you can add an assertion to the default case.
For eg:
import java.io.*;
public class AssertionTest3 {
public static void main(
String argv[]) throws IOException {
System.out.print("Enter your marital status: ");
int c = System.in.read();
switch ((char) c) {
case 's':
case 'S': System.out.println("Single"); break;
case 'm':
case 'M': System.out.println("Married"); break;
case 'd':
case 'D': System.out.println("Divorced"); break;
default: assert !true : "Invalid Option"; break;
}
}
}
You assume that marital status will never be other than s,S,m,M,d,D.To test whether this assumption you can use assert.
Hope this helps.
Niara.