File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion Question" Watch "Assertion Question" New topic
Author

Assertion Question

Tayitu Betule
Ranch Hand

Joined: Dec 18, 2009
Posts: 39
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

Joined: Jun 21, 2008
Posts: 423
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

Joined: May 10, 2010
Posts: 1
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

Joined: Feb 02, 2010
Posts: 247
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)


SCJP 6
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

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

Hope this helps,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
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.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

Hi
Tayitu

Try this


javac AssertTest.java //for compiling your code

java -ea AssertTest //for running your code



definetly this may solve your problem


Thanks & Regards
Sumit Kothalikar
Tayitu Betule
Ranch Hand

Joined: Dec 18, 2009
Posts: 39
Thank you guys. It did work when running it with -ea.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion Question