what is use of assert keyword in java..?

what is use of assert keyword in java..? ..

Answer / me

"assert" statements are part of the Java assertion feature
introduced in Java 1.4. Java assertion feature allows
developer to put "assert" statements in Java source code to
help unit testing and debugging.

An "assert" statement has the following format:

assert boolean_expression : string_expression;

When this statement is executed:

* If boolean_expression evaluates to true, the statement
will pass normally.
* If boolean_expression evaluates to false, the
statement will fail with an "AssertionError" exception.

Here is a good example of an "assert" statement used to
check an invariant:

// Day-of-week calculator
// by FYICenter.com

public class DayOfWeek {
private int days = 0;

// main method for testing purpose
public static void main(String[] arg) {
int d = Integer.parseInt(arg[0]);
DayOfWeek o = new DayOfWeek(d);
System.out.print("Day of the week: "+o.getDayOfWeek());

// constrcutor
public DayOfWeek(int d) {
days = d;

// calculate day of the week
public String getDayOfWeek() {
if (days % 7 == 0) {
return "Sunday";
} else if (days % 7 == 1) {
return "Monday";
} else if (days % 7 == 2) {
return "Tuesday";
} else if (days % 7 == 3) {
return "Wednesday";
} else if (days % 7 == 4) {
return "Thursday";
} else if (days % 7 == 5) {
return "Friday";
} else {
assert days % 7 == 6 : days;
return "Saturday";

How To Compile and Run Java Programs with "assert" Statements?

If you are using "assert" statements in your Java program
for unit testing and debugging, you need to compile and
execute the program with extra command options to use them:

* Compilation - "javac -source 1.4 MyClass.java". This
tells the compiler to accept source code containing assertions.
* Executionn - "java -ea MyClass". This tells the JVM to
not skill "assert" statements. "-ea" is the same as

Here is an example of compiling and executing the sample
program, DayOfWeek.java, presented in the previous question:

javac -source 1.4 DayOfWeek.java

java -ea DayOfWeek 1
Day of the week: Monday

java -ea DayOfWeek 6
Day of the week: Saturday

java -ea DayOfWeek 14
Day of the week: Sunday

java -ea DayOfWeek -2
Exception in thread "main" java.lang.AssertionError: -2
at DayOfWeek.getDayOfWeek(DayOfWeek.java:34)
at DayOfWeek.main(DayOfWeek.java:11)

As expected, the code does not handle negative numbers correctly

Is This Answer Correct ?    8 Yes 3 No

Post New Answer

More Struts Interview Questions

Explain about struts dispatch action?

0 Answers  

what is the disadvantages of using DynaActionForm in struts framework?

5 Answers   HCL,

what is is the use DynaActionForm?

3 Answers   TCS,

What is the default location of result pages and how can we change it?

0 Answers  

How can we get Servlet API Request, Response, HttpSession etc Objects in action classes?

0 Answers  

what is ACID test for fresh engineers??what is the pattern??

0 Answers  

wat happen when the connection is nt closed in jdbc n what vl happen when i interchange in executequery n execute statements

2 Answers  

how can i fetch combo box value into my DAO using struts 2?

3 Answers  

why servlet is used as controller not JSP?

9 Answers   MCS,

What is MVC?

6 Answers   Infosys,

advantages& 5 disadvantages of MVC architecture

6 Answers   TCS, Satyam,

What is the differnce between 2-tier architecture and 3- tier architecture in java?

3 Answers