Hello people!
I've several doubts about assertions.
First one syntax:
Based on what book says (Kathy Sierra and Bert Bates), there are 2 ways "simple" and "way simple".
(using book examples)
Paying attention to the last 3, in the first block of code (lines 10,11 and 12). It's the first time I see it so It gets me confused. What I have clear is the keyword followed by the boolean expression but, the second with the second memeber in the syntax ... I don't get it. Can we place anything there? With anything I mean, any value, method call or even returning values from method calls? (Which is the rule in summary, if there's any, regarding syntax ).
---
The second question , do assertions trigger (happens) when the boolean expression is false?
---
The third question is about, what is does needed to be known about this subject. I've read it all and yet I haven't got it clear at all. I believe the important part (in order to be focused on the exam) is to know :
1) you can't use assertions for public methods to validate arguments.
2) the opposite as 1), can use assertions for private methods to validate arguments
3) you can't use them for command line arguments (this one looks quite obvious)
4) you can use them to check cases you know are never (ever) suppose to happen.
Point 4 gives an example:
It's a way to show you simply can't use them in a block of code that will never be reached, am I right?
The explanation:
If you assume that a particular code block won't be reached, as in the preceding
example where you assert that x must be either 1, 2, or 3, then you can use assert
false to cause an AssertionError to be thrown immediately if you ever do reach
that code. So in the switch example, we're not performing a boolean test—we've
already asserted that we should never be there, so just getting to that point is an
automatic failure of our assertion/assumption
If x value doesn't take any value of those, it reaches defaul and code fails, right?
Thank you in advance for your help, knowledge and of course patience