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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Assertion Q
 
Similar Threads
How to turn on assert in TextPad ?
Assertion
assert
Assertion topic
Qn on assertion.