• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

If compiler can automatically cast int to byte, then why not long to int?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte b = 10; is ok because 10 is in range of byte values

---but---

long ln = 10;
int i = ln; is not ok even though 10 is in range of int values

why?
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even though it may look to the human observer is if it should be obvious to the compiler that this cast will work, it's not clear that much is to be gained by having the compiler recognize it. What if there is one other statement between the two, or several, even though they don't change the value of ln? Should the compiler still recognize this?
 
Tobie Henderson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There may be something to gain from int i = 10L;

Or at least as much as can be gained by byte b = 10;

Originally posted by Ulf Dittmer:
Even though it may look to the human observer is if it should be obvious to the compiler that this cast will work, it's not clear that much is to be gained by having the compiler recognize it. What if there is one other statement between the two, or several, even though they don't change the value of ln? Should the compiler still recognize this?

 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic