• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Casting Related Doubt

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai ranchers,

i know the fact that when a double number is divided by 0 it doesnt result in exception but it results in value of Infinty...
but the problem i have is when i try to type cast this value of infinity to byte...it results in value of -1 ....
sample code...

double d1 = 1.0;
double d2 = 0.0;
d1= d1/d2; //d1 results in Infinity
byte b = (byte) d1; //b gets assisned -1

can u help me understand y it gets this value assigned..
thnks in advance
 
Sheriff
Posts: 22853
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know how infinity is defined in bits, but I'm thinking it's something like 0111 1111 1111 ... 1111 1111 1111. If you cast it to a byte, you cut off everything except for the last part: 1111 1111. This is -1.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic