• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding assertion

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic