Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes on assertions 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 "on assertions" Watch "on assertions" New topic

on assertions

velan vel
Ranch Hand

Joined: Nov 15, 2005
Posts: 137
here bydefaultly the assertions are disabled
so it should print finished
but why this code is giveing compile time error

1. public class Test {
2. public static void main(String[] args) {
3. int x = 0;
4. assert (x > 0) ? "assertion failed" : "assertion passed";
5. System.out.println("Finished");
6. }
7. }
What is the result?
A. finished
B. Compilation fails.
C. An AssertionError is thrown and finished is output.
D. An AssertionError is thrown with the message "assertion failed".
E. An AssertionError is thrown with the message "assertion passed".
Answer: B

velan vel
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
assert (x > 0) ? "assertion failed" : "assertion passed";

the two syantaxes for assert statments are

1. assert expression1;
2. assert expression1:expression2;

u r using something like (but not exactly) case 2. the expression is incorrect...and does not exqactly return boolean value

in ur case < (x>0) ? "assertion failed" ) turns out to be expression 1
and it doesnt return a boolean

hence compiler error

SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
I agree. Here's the link:
subject: on assertions
It's not a secret anymore!