Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding assertion

 
B pandoo
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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;
try{

System.out.println(isValid());
assert isValid() i=i/4);
System.out.println("inside i"+i);
}
catch(AssertionError e){
System.out.println("i=" + i);
}
}
public static boolean isValid(){
i=i*2;
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pandoo,
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
System.out.println(isValid());
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:
>javac test.java
>java -ea test

How to enable assertions in eclipse?
I dont know. Some ranchers might have information on this.
 
Satish Kota
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
false
inside i40
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Yours,
Bu.
 
Satish Kota
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic