• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help

 
sweety singh
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
 
Serg Masow
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic