This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding assertion" Watch "Regarding assertion" New topic

Regarding assertion

B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37

I want to ask a basic Question regarding assertion.
usually assert syntax for java1.5 will be

assert (booleanvalue);
assert (booleanvalue): expression;

in the 2nd syntax,I want to know that if the booleanvalue is false,whether the expression gets evaluated?
ie,once the boolean value is false,we get assertionerror(run with -ea).In that case,i want to know whether the expression will be evaluated or not.

I want to know one more thing,how to compile a program with assertion enabled both in commandprompt and using eclipse.

public class Assertest{
static int i =10;
public static void main(String[] args){

i= i*2;

assert isValid() i=i/4);
System.out.println("inside i"+i);
catch(AssertionError e){
System.out.println("i=" + i);
public static boolean isValid(){
return false;

when i compiled the program,i got the answer as i=20.can anyone explain this.i thought the answer will be i=40.

I want to know the answer for the above Question with assertion enabled and assertion disabled.Please explain the difference when assertion enabled and assertion disabled.It is confusing for me.

Advance thanks to the group for the effort put in.
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
In your case the assertions are enabled. So when the programe executes it reaches the line
i= i*2;
and the value of i is 20
and now it reaches
isValid() method is called which caused i to become 40.
Nothing unusual so far.

Going by your o/p Assertions are enabled in your machine hence the next line will be executed
assert isValid() : i=i/4 ;
The isValid method is called which causes i to change to 80. As this method returns false the expression after boolean expression, in your example i=i/4 will be evaluated to a value, and the result is i=80/4 which is 20.
the control goes to catch block where i=20 will be displayed.

How to compile and run from command prompt:
>java -ea test

How to enable assertions in eclipse?
I dont know. Some ranchers might have information on this.

SCJP 5.0 77%
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
when assertions are disabled:
The whole assert statement will be ignored
assert isValid() : i=i/4 ;
The above statement would not execute and the output is:
inside i40
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

With eclipse (windows):
Right click on the window with your AssertTest code,

Run as , green arrow Run,

in the window select Arguments and type -ea in the VM arguments panel.

The insertions are enabled now for this class.


all events occur in real time
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
For your information. The following are not considered to be appropriate in the usage of assertions:

1. The state of programe must be the same as it was before the execution of assertions. So any call to method during assertion execution which changes variable values is not considered good practice, as not all machines will have assertions enabled and you dont want your code to behave differently in different machines.

2. It is not a good practice to catch AssertionsError even though you can do it technically.
I agree. Here's the link:
subject: Regarding assertion