This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
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!