• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implicit narrowing conversions

 
Jimmy Blakely
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3764
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should help you: http://enthuware.com/jqplus/FAJQuestions.html
-Paul.
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
leena rane
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic