Am I correct in concluding that integer literals are the only primitive literals that the compiler performs implicit narrowing conversions on at compile time? For example: short s = 12; // okay...literal 12 implicitly narrowed char c = 15; // okay...literal 15 implicitly narrowed byte b = 8.0; // not okay....no implicit conversion for double literal int i = 12.0; // not okay....same as above float f = 14.0; // believe it or not...this isn't allowed either
Ragu Sivaraman
Ranch Hand
Joined: Jul 20, 2001
Posts: 464
posted
0
Originally posted by Jimmy Blakely: Am I correct in concluding that integer literals are the only primitive literals that the compiler performs implicit narrowing conversions on at compile time? For example: short s = 12; // okay...literal 12 implicitly narrowed char c = 15; // okay...literal 15 implicitly narrowed byte b = 8.0; // not okay....no implicit conversion for double literal int i = 12.0; // not okay....same as above float f = 14.0; // believe it or not...this isn't allowed either
Yup, BSC <----I Implicit narrow only for integers Even within BSC we need explicit casting oh btw, BSC is Byte, Short , Char Note: The value of int MUST be with the range of the destination type HTH [This message has been edited by Ragu Sivaraman (edited August 13, 2001).]
Paul Anilprem
Enthuware Software Support
Ranch Hand
>Am I correct in concluding that integer literals are the only >primitive literals that the compiler performs implicit narrowing >conversions on at compile time? Yes,that's right . For example: short s = 12; // okay...literal 12 implicitly narrowed char c = 15; // okay...literal 15 implicitly narrowed byte b = 8.0; // not okay....no implicit conversion for double literal int i = 12.0; // not okay....same as above float f = 14.0; // believe it or not...this isn't allowed An integer literal can be assigned to a byte,short,char if value is within bounds.(this answers 1&2) Definitely they can't take double values. (this answers 3) An integer cannot take long,float or double values(this answers4) float cannot take a double value. 14.0 is a double literal. try this: float f=14.0f; it should work, (this answers 5)
hope this helps , Best regards, leena.
Guoqiao Sun
Ranch Hand
Joined: Jul 18, 2001
Posts: 317
posted
0
And remember that is is always possible to assign an integral type value to a float type value: For example:
Case // A is feasible although some precision might be lost. Guoqiao [This message has been edited by Guoqiao Sun (edited August 13, 2001).]
Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum