• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Help

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
}
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sweety, please use a meaningful subject line instead of just "Help".
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic