File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implicit narrowing conversions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Implicit narrowing conversions" Watch "Implicit narrowing conversions" New topic
Author

Implicit narrowing conversions

Jimmy Blakely
Ranch Hand

Joined: Jul 10, 2001
Posts: 57
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
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

Joined: Sep 23, 2000
Posts: 3253
    
    2
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
leena rane
Ranch Hand

Joined: Aug 13, 2001
Posts: 280
>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
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
 
 
subject: Implicit narrowing conversions
 
Similar Threads
confusing stuff in wrapper classes
jtips question
narrowing
char c = 32 //Why is it legal?
Casting