a narrowing primitive conversion may be used if all of the following conditions are satisfied:
1.The expression is a constant expression of type byte, short, char or int. 2.The type of the variable is byte, short, or char. 3.The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.
In your code below,
the type of i is known at compile time to be 'int'. However #1 in the quote above is not satisfied and it fails. It would require an explicit cast.
Try the following
Joined: Sep 28, 2004
Thanks Ram & David for the reply. I understood the concept.
Since the variable and all the constants are integers, the expression "i=1/3" will be evaluated using integer arithmetic. In integer arithmetic, 1 divided by 3 is 0, a perfectly valid integer.
I suspect that if you wrote "i=1f/3f", for instance, it would not compile, because 1f/3f would be computed using float arithmetic. Haven't tried though. Go on, someone.
Just out of interest, in your float expression "f=1/3", what value did f get? It could get 0.0 or 0.333..., depending on whether the integers 1 and 3 get converted to float before the division. I guess a careful reading of the Java specs would tell us, if we could be bothered!
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.