File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion Q 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 Q" Watch "Assertion Q" New topic

Assertion Q

Sai Charan

Joined: Apr 24, 2006
Posts: 22
Please explain the output of this code. Thanks in advance.

class ATest
public static int y = 0;
public static void main(String[] args)
int t = 2;
assert t <4 : foo(7);
assert t > 1: bar(8);
assert t > 1: foobar(10);

public static int bar(int x) {
return y = x;

public static int foo(int x) {
return y = x;

public static boolean foobar(int x) {
return false;


Running the code with assertions enabled as below giving output?
java -ea ATest
[ May 26, 2006: Message edited by: Sai Charan ]
warren li
Ranch Hand

Joined: May 23, 2006
Posts: 128
all of the following:
assert t <4 : foo(7);
assert t > 1: bar(8);
assert t > 1: foobar(10);

passes the assertion test since t is 2, so
these 3 methods never get executed.

so the next line "done" is immediately reached.

SCBCD 1.3: 94%<br />SCWCD 1.4: 91%<br />SCJP 5: 95%
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
the assert statement part after : is executed when assert got fialed. As in your case all three assert get paased due to which none of three method are called.

"Know where to find the solution and how to use it - that's the secret of success."
I agree. Here's the link:
subject: Assertion Q
It's not a secret anymore!