This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strange Assertion Behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strange Assertion Behaviour" Watch "Strange Assertion Behaviour" New topic

Strange Assertion Behaviour

Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
In the following example from Dan Chisolm:

I would have thought that this wouldn't compile because the expression i1 = j1 + x1 doesn't return a boolean, it returns an int. I thought that the assert() method needed to receive a boolean. So what is going on here? I'm a little confused.
marc weber

Joined: Aug 31, 2004
Posts: 11343

The complete expression is (i1 = j1 + x1) < 6; which does return a boolean.
[ February 10, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193

You're right that (i1 = j1 + x1) doesn't return a boolean, but (i1 = j1 + x1) < 6 does. It's saying "assign i1 the value of j1 plus x1 and then compare the result to see if it's less than 6.

(i1 = j1 + x1) < 6

translates to

result of addition assigned to i1 compared with 6

and that evaluates to a boolean.


Rational Pi Blog - Java, SCJP, Dev Bits-
Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
clearly my brain isn't working today. Thanks guys.
I agree. Here's the link:
subject: Strange Assertion Behaviour
It's not a secret anymore!