wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertions clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertions clarification" Watch "Assertions clarification" New topic

Assertions clarification

Srinivasa Kadiyala
Ranch Hand

Joined: Jun 10, 2004
Posts: 237
Pl go through the following code:

<<<<< code starts here >>>>>>>>>>>>>>

public class TestAssertions{

public static void main(String args[]){

int x=2;
assert (x==2): new TestAssertions().go(x);


public int go(int x){
System.out.print("in go()...");
return x;



<<<<<<<<<<<<<<< code ends here >>>>>>>>>>>>>>>>

javac -source 1.4 TestAssetions.java >>> Success


java -ea TestAssertions >>> Success


Confused with the output. I am expecting the following..

in go()... 4 4

why the out put is different?

Thanks in advance

patrick J. d'cousta

Joined: Oct 25, 2005
Posts: 24
Assertions are enabled but the condition
x==2 will be true so the output is 2.
put assert(x>2) and run with assertions enabled
Srinivasa Kadiyala
Ranch Hand

Joined: Jun 10, 2004
Posts: 237
Hi Patrick

Thanks for poiting the logic.

Hereunder is the result:

C:\temp>javac -source 1.4 TestAssertions.java

C:\temp>java -ea TestAssertions
in go()...4Exception in thread "main" java.lang.AssertionError: 4
at TestAssertions.main(TestAssertions.java:7)

It is sorta covered in the JavaRanch Style Guide.
subject: Assertions clarification
Similar Threads
Flow control!
switch statement
What is the output?
Need Help With For Loops
I thought I understood this stuff.....