• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Type Casting in Java with Primitive value

 
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why printing value of b2 give value as 64 how??


 
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yogesh doshi wrote:Why printing value of b2 give value as 64 how??



Java uses the Twos Complement format to represents integers. However, since the sign bit isn't set in your example, you don't need to fully understand it...


So, to answer your question, ignoring the negative representation of Twos Complements....

The integer value of 123456 in decimal is 00000000 00000001 11100010 01000000 in binary.

With Java, when you explicit cast (and narrow), the higher bytes are simply discarded. So, the byte value of the b2 variable, after discarding the higher bytes is 01000000 in binary. And of course, this value is 64 in decimal.

Hope this helps,
Henry



 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic