File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion Q" Watch "Assertion Q" New topic
Author

Assertion Q

Sai Charan
Greenhorn

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);
System.out.println("done");
}

public static int bar(int x) {
System.out.println("bar");
return y = x;

}
public static int foo(int x) {
System.out.println("foo");
return y = x;

}
public static boolean foobar(int x) {
System.out.println("foobar");
return false;

}
}

Running the code with assertions enabled as below giving output?
java -ea ATest
done
[ 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: http://aspose.com/file-tools
 
subject: Assertion Q
 
Similar Threads
Assertion
Qn on assertion.
assert
Assertion topic
How to turn on assert in TextPad ?