aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertions clarification" Watch "Assertions clarification" New topic
Author

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);
System.out.print(x);

}

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

}

}


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

compiled:
javac -source 1.4 TestAssetions.java >>> Success

Run:

java -ea TestAssertions >>> Success

output:
2

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

in go()... 4 4

why the out put is different?

Thanks in advance


Srini
patrick J. d'cousta
Greenhorn

Joined: Oct 25, 2005
Posts: 24
Sir,
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)

Thanks....
 
wood burning stoves
 
subject: Assertions clarification