There is this question in Dan's mock tests Which of the following class instance creation expressions would generate a compile-time error? a. new Short(1) b. new Short('1') c. new Short('b' - 'a') d. new Short((short)1 - (short)2) e. new Short((byte)1) f. new Short((short)1) The answers are a,b,c,d Why type conversions in e and f are valid when its invalid in d? Any help?? Thanks.
The result is in int, where only short / string is expected. Hence the failure. The same is the case for option 1 also. Where nos casting is done, it is kept as int.
Originally posted by geeta rai: Actually my question was that why options 'e' and 'f' are valid while option 'd' is not? Doesn't (short)1 is typcasted to short?
The resulting type of any math operation is at least int, even if byte operands only are involved.