Great explanation @oddz;
By using (int) you know exactly what you are going to get, either a positive integer or 0.
It all comes down to how you have presented options to a user on a form on your site.
a) Did you give them a pick list of numbers to pick from, or a pre-defiined element with a set integer (usually a hidden field with an
b) Did you give them a text box into which they can enter just about anything, but your GUI hints specify a number.
If you do not get an integer, does that mean they:
1) could have made a mistake
2) altered a copy of the html and re-submitted it
If you did not get an integer after :
a) then 1) -- this is not possible without tampering, same as a) then 2) -- this is bad
a) then 2) -- this is bad
b) then 1) -- then you should question your motives, or be kind an throw back an error msg.
b) then 2) -- this is bad
So, when you are expecting an integer, and after typecasting it equates to 0 then this is generally a bad sign, and you should abort and get rid of the user.
As a developer it is also very easy to use and recall, that is why you will see it again and again.