• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Literals assignements

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why

byte i = 123; //(integer)

compile and

int i = 123L; //(long)

not compile?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte holds upto 127 so 123 is small enough for a byte
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic