This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

int -> byte , short , char

 
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte b = 23 ;

a literal ( should not be fraction ) is always int by default . But compiler automatically cast if the literal is in limit of byte . So above statement is perfect .

byte b1 = 4;
byte b2 = 6;
byte add = b1+b2;

why it doesn't work . the result will come 10 ( it is int .. i am agree ) & it is in limit of byte , do why not compiler automatically cast in this case .

help needed .
thanks .
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler doesn't cast it automatically, because b1 & b2 are variables, meaning they can vary at some time. So, the compiler is bothered. It is worried that problems might occur if the values of b1 and/or b2 changes at runtime, such that the sum of them will exceed the limits of byte.

If you can convince the compiler that the values of b1 & b2 will never change, the compiler will be happy and will keep quite.
You can convince the compiler by saying

[ January 06, 2005: Message edited by: Mani Ram ]
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ultimate explanation .
thank you very much .
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
float f = 2.3;

why in this case compiler doesn't cast double to float implicitly . As 2.3 is in limit of float .

thanks .
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!