Best Answer

assert is a keyword added in Java version 1.4.

assert tests the programmer's assumption during development without writing exception handlers for an exception. Suppose you assumed that a number passed into a method will always be positive. While testing and debugging, you want to validate your assumption. Without the assert keyword, you will write a method like:

private void method(int a) {

if (a >= 0) {

// do something that depends on a not being negative

} else {

// tell the user that a is negative



This is simple exception handling; consider the case of big one. Assertion will come into the picture when you don't want to take the time to write the exception handling code.

Consider the above program with assertion:

private void method(int a) {

assert (a>=0); //throws an assertion error if a is negative.

// do stuff, knowing that a is not negative


In this program, assert (a>0) will pass when 'a' is only positive. Isn't this much cleaner than the previous example? If the value of 'a' is negative, then an AssertionError will be thrown.

There are two types of assertions:

  1. Simple
  2. Really simple

Example of a really simple assertion:

private void method() {

assert (x>0);

//do more stuff


Example for simple assertion:

private void method() {

assert (x>0) : "X is" + x ;

// do more stuff


The difference between these two is that the simple assertion - the second example - appends the expression after the colon to the error description in the stack trace.

Note: assertion code - in effect - evaporates when the program is deployed.

For assertion first you need to invoke the assertion.

Asserts are disabled by default in the JVM. In order to run a program with asserts you must use the -ea command line parameter (i.e. java -ea AssertTest).

Even though your compiler suggested it was using java version 1.4, it wasn't actually because you compiled using the "javac [files]" command instead of the "javac -source 1.4 [files]" command. Assertions only work in version 1.4 and later versions.

When you are using assert keyword, you have to enable it. Otherwise it wont give any result.

java -enableassertions classfile

User Avatar

Wiki User

โˆ™ 2015-07-17 17:35:43
This answer is:
User Avatar
Study guides
See all Study Guides
Create a Study Guide

Add your answer:

Earn +20 pts
Q: What does the assert keyword do in Java?
Write your answer...
Related questions

Is check a keyword in java?

No, 'check' is not a keyword in java language.

Is null keyword in java?

No null is not a keyword. null is considered to be a "literal" in Java.

Is float is a keyword in java?

yes, float is keyword and data type in java

What does the verify keyword do in Java?

"verify" is not a Java keyword. I believe the link, in related links, has the complete list of Java keywords.

Provide syntax for assert keyword?

assert (boolean expression); Example: assert (a >= 0);

What is java's keyword for the integer data type?

"int" is the keyword for integer

What is literal in java programming?

Literal in java are L, F, null, true, false These act as keyword(have special meaning in java) but these does'nt comes under the category of Java Keyword.

Which is a valid keyword in java?


What is the command to execute a Java Program?

"java" is the keyword/command used to execute Java Programs

How does the final keyword affect the behavior of Object type in Java?

In Java, the final keyword specifies that the object created cannot be further redefined or derived.

What is the difference between assert keyword and if keyword in java?

The main thing you should keep in mind is that the if-else statement should be used for program flow control and the assert keyword should only be used for testing purposes. You should never use asserts to actually perform any operation required for your application to work properly. According to Sun's official Java documentation: "Each assertion contains a boolean expression that you believe will be true when the assertion executes." For example, if you have a method which is supposed to return a double value in the range [-1.0,1.0], you can use the following two statements to test this: assert value = -1.0; If either of these assertions returns false, an error will be thrown to let you know there's a problem.

What is the use of the this keyword in Java?

Using this keyword in Java is a way of referencing something which has been used earlier in the program. This is very useful knowledge to have when the program is long.

What do you mean by this keyword in java?

"this" is a Java keyword that references the current object. Any part of the object(instance variables, methods, constructors) can be accessed by calling this.[member].

What is foreign keyword in java?

There is no "foreign" keyword in Java, however, there is a native keyword that declares native methods in a native language, such as C or C++.For full list of keywords in Java see related question.

What is a new keyword in java methods?

new is a keyword to create a instance of object any class.

What are the uses of throw keyword in java?

The throw keyword is used to throw User Defined Exceptions explicitly.

How do you throw exception in java?

we do it using the throw keyword.

Define the keyword public in java?

public is an access modifier in Java that allows all classes and methods uncontrolled access to the class or the variable that is defined with the keyword public in their definition.

Why use catch keyword in java?

The catch keyword is used in Java exception handling along with try blocks. It is used to catch and handle exceptions that are thrown inside the try block

How java use extern key word which is used in C plus plus?

No extern keyword in Java.

What does the this keyword entail in java?

The This keyword references the current object. It can be used in an instance method or constructor for an object.

Use of this keyword in java?

this keyword is used to avoid complication between instance variable and local varible.we identify instance variable by using this keyword(e.g: this.a=a;)

Which keyword is used in data declaration to indicate a constant and unchanging value?

In java, the final keyword is used to mark a variable as unchangeable.

What is the return keyword in Java?

this is the type of the value that the method returns to its caller

Which keyword is used to declare exceptions that can possibily occur in java?