What does the assert keyword do in Java?
Answer:
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:
- Simple
- 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).
Answer
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.
Answer
When you are using assert keyword, you have to enable it. Otherwise it wont give any result.
java -enableassertions classfile
First answer by ID0416112122. Last edit by APerson241. Contributor trust: 31
[recommend contributorrecommended].
Question popularity: 54
[recommend question].
Can you answer these Java Programming questions?
Related topics:
Why join?
Joining is free and easy. You can still be anonymous; just choose any username and password.- Get notified about updated answers
- Follow your favorite categories
- Get credit for your contributions
- Customize your profile
- Answer questions more easily
Lost your password?
You may already have an Answers.com account.
Click here to connect your accounts.
If you don't want to connect accounts, you can start a new one from scratch.
Click here to connect your accounts.
If you don't want to connect accounts, you can start a new one from scratch.
Minor details...
Connect your accounts...
Why do we need your email address?
We will use your email address to send you updates (if you request them) about questions you ask, answer or track, and to help you retrieve your password if you forget it.Your email address will not be used for any other purpose without your permission.
Already have an Answers.com account? Connect your accounts!
By doing so, you include all of your history (contributions, messages, profile) from your Answers.com account in your Facebook account.If you don't connect accounts, your new account will be starting from scratch.
Home
Welcome
Help center
Browse categories
- Animal Life
- Business & Finance
- Cars & Vehicles
- Entertainment & Arts
- Food & Cooking
- Health
- History, Politics & Society
- Hobbies & Collectibles
- Home & Garden
- Humor & Amusement
- Jobs & Education
- Law & Legal Issues
- Literature & Language
- Relationships
- Religion & Spirituality
- Science
- Shopping
- Sports
- Technology
- Travel & Places
- WikiAnswers Local
Random question
Community forum

