June 10, 2008 at 3:19 PM
Hi friend,
The assertion statement has two forms.
assertExpression1;
assert Expression1:Expression2;
The first one is a simpler form that takes a boolean expression as an argument. The expression is the one the programmer wishes to assert as true. If the assumption fails, the expression evaluates to be false which means the assertion failed. In case the expression succeeds the program continues normally.
The second form is the more descriptive one. The first argument takes a Boolean expression, while the second expression would be the resulting action to be taken if the assertion fails. The Expression2 should be a value and can also be a result of executing a function. The compiler would throw an error if the second expression returns a void value.
import java.io.*;
import java.io.IOException;
public class AssertionExample{
public static void main(String argv[]) throws IOException{
System.out.println("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;
// default: Assertion.assert(!true, "Invalid Option");
// break;
}
}
}
-----------------------------------------------------------
Read for more information.
http://www.roseindia.net/javacertification/scjp5/assertionsexample.shtmlThanks
Related Tutorials/Questions & Answers:
ModuleNotFoundError: No module named 'assertions'ModuleNotFoundError: No module named '
assertions' Hi,
My Python... '
assertions'
How to remove the ModuleNotFoundError: No module named '
assertions' error?
Thanks
Hi,
In your python environment you
Advertisements
ModuleNotFoundError: No module named 'assertions'ModuleNotFoundError: No module named '
assertions' Hi,
My Python... '
assertions'
How to remove the ModuleNotFoundError: No module named '
assertions' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'assertions'ModuleNotFoundError: No module named '
assertions' Hi,
My Python... '
assertions'
How to remove the ModuleNotFoundError: No module named '
assertions' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'assertions'ModuleNotFoundError: No module named '
assertions' Hi,
My Python... '
assertions'
How to remove the ModuleNotFoundError: No module named '
assertions' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'assertions'ModuleNotFoundError: No module named '
assertions' Hi,
My Python... '
assertions'
How to remove the ModuleNotFoundError: No module named '
assertions' error?
Thanks
Hi,
In your python environment you
assertions - Java Beginnersassertions can you explain me how and when
assertions are usefull with an example..
Thanks in advance Hi friend,
The assertion statement has two forms.
assertExpression1;
assert Expression1:Expression2