Hugo Kornelis (3/25/2010)
Good question, but unfortunately the explanation is not entirely correct. Especially this part is very misleading:
Also the preceding space is not considered while performing the operation.
I agree. It's a good question and the answer is right, but that part of the explanation is wrong. Not sure why you say "especially that part". Are you suggesting that some other part is wrong too?
As you have seen, there is no leading space to be considered anywhere. However, even if there was, the words "is not considered" is misleading. I think we would all agree that converting the values '5', ' 5' and ' 5 ' to integer should all result in the value 5 - not because we expect the leading and trailing spaces to be "not considered", but because considering them or not is irrelevant, as they have no impact on the interpretation of the string as a number.
I don't much like saying that the reason ' 5 ' represents the integer 5 despite its having leading and trailing spaces is that "considering them or not is irrelevant"; the reason is that when we consider them we see that they have no impact on the interpretation of the string as a number - if we didn't consider them how would we know that they have no effect, that it was safe to say the string represents the number 5?