This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
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 Introducing JavaFX 8 Programming this week in the JavaFX 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 >>> 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

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

I agree. Here's the link:
subject: Assertions clarification
jQuery in Action, 3rd edition