• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Conversion Confusion

 
Ranch Hand
Posts: 92
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question :  Long has more memory than int . During converting how is that possible, suppose long max value is 10 digits and int has 8 digits  i want to convert long into int and i put value = 9876321345 (10 digits i.e Max Value) will it convert it into int . While int contain only 8 digits storage  >> How this type of conversion happen in java ?

 
Saad Zahoor
Ranch Hand
Posts: 92
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry that is minValue
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually their are some rules for the possible conversion in Java® Language specification.
For the question-suppose you have an long value as:
long longValue = 9_223_372_036_854_775_807L → which is (2^63)-1
in bits it is
01111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
if you would try this:
int intValue = (int) longValue
this is a type of narrowing conversion(long to int) here you would of course going to loose some bits information.here you would loose the most significant 32 bits because int can only represent 32 bits so the intValue here is:
intValue: 11111111 11111111 11111111 11111111
[ loosed bits: 01111111 11111111 11111111 11111111 ]
this is a binary form of intValue.you can see it is a negative number as in java negative numbers are represented by 2's complement notation.so the bits above is a 2's complement representation which is (-1) in decimal.for a proof run this program:


Hope it helps!

Kind Regards,
Praveen.
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic