• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion Question

 
Tayitu Betule
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following question is from Master Exam. Why does the code prints "done" when line 17 resolves to false? Please explain your answer. Thanks a lot!


import java.lang.*;

public class AssertTest{
public static int y;
public static int foo(int x){
System.out.print("foo ");
return y=x;
}

public static int bar(int z){
System.out.print("bar ");
return y=z;
}
public static void main(String[] args){
int t=20;
assert t < 4:bar(7); //line 17
assert t > 1:foo(8);
System.out.println("done");
}
}

//The program print "done"

 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you run this program with option -ea (enable assertions),
it throws AssertionError exception at line 17.
If you run it with disabled assertions (without -ea or -enableassertion), it prints 'Done'.
 
Giorgio Francescangeli
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Tayitu Betule,

I am studying for the SCJP exam, and I am not an expert.

To reply to your post I think the code prints out "done" because the assertions are ignored,
there is nothing that tell us that the assertions are enabled, thus at run time these lines of code are not executed.

To enable assertions it needs to be specified with a flag in the command line:
java -enableassertions AssertTest
or with the short flag, which is equivalent
java -ea AssertTest

Regards,

Giorgio
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tayitu,

JavaRanch message posting has a feature called "Code" that will display your code in the following manner:


For the assertion problem, Giorgio and Ireneusz are right about the -ea flag. If you are using Eclipse IDE, then go to "Run Configurations..." and set "-ea" as the VM argument under the Arguments tab. Then run AssertTest as a Java application and you will get:

bar Exception in thread "main" java.lang.AssertionError: 7
at AssertTest.main(AssertTest.java:17)
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Please use code tags while posting the question, rather then quoting it.
It makes the question hard to read.

Hope this helps,
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to take Larry's IDE advice even farther (further?):

Don't use an IDE to study for the exam!!!

The entire Sun SCJP exam was coded with text editors, and tested from the command line! A lot of times IDEs do things 'behind the scenes', without telling you - this can often lead to misleading results for SCJP candidates.

hth,

Bert

p.s. fwiw, we didn't use an IDE to write the code for either K&B or HF Java - we used only text editors and the command line.
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Tayitu

Try this


javac AssertTest.java //for compiling your code

java -ea AssertTest //for running your code



definetly this may solve your problem
 
Tayitu Betule
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys. It did work when running it with -ea.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic