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 Literals assignements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Literals assignements" Watch "Literals assignements" New topic
Author

Literals assignements

John Blutarsky
Greenhorn

Joined: Jun 24, 2005
Posts: 5
Why

byte i = 123; //(integer)

compile and

int i = 123L; //(long)

not compile?
Raghuveer Kumarakrishnan
Ranch Hand

Joined: Mar 13, 2005
Posts: 32
byte holds upto 127 so 123 is small enough for a byte


Raghu<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1558
Hi john!
Whether it be 123 or 143
All integral numbers are "int" by default.
Similary remember this all floating points be it 12.3 or 89.7382 they are "double" by default.

Integers can be assigned to byte, short or character provided they are in their acceptable range. Your case, 123 fits in byte so ok.
Next is trying to fit a "long" into an "int" which is not possible. Hope this helps...
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.2


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Literals assignements