File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help " Watch "Help " New topic
Author

Help

sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
hi...
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

2. can anyone give me an example for not using assertions to validate command line arguments.

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!
}
Niara Bhonde
Greenhorn

Joined: Jan 02, 2008
Posts: 10
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Sweety, please use a meaningful subject line instead of just "Help".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 49
Hi sweety,

there is a fine difference between command line
java -ea com.geelcsanonymous.Foo
and
java -ea:com.geeksanonymous.Foo

The first command line enables assertions generally and load the class com.geelcsanonymous.Foo for execution
The second command line is incomplete because it just enables assertions for class com.geeksanonymous.Foo but doesn't specify the class for execution.


SCJP 6.0 [95%] OCP EJBD 6.0 [93%]
 
Consider Paul's rocket mass heater.
 
subject: Help
 
Similar Threads
assertions doubt
K&B Chapter 4: Assertions
Assertion Doubt
Assertions
post and pre increment opeartor