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

cast between long and int.

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you automataiclly convert a long primitive to a int ,if the value of the long is samll enough to fit the int?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
You can carry out narrowing of conversion if range of the variable is not a problem. If range of long variable for conversion is within range of interger then narrowing of conversion is possible.
Regards
Sandip
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James
If by automatically convert you mean without a cast then no you can't do it (not from long to int anyway. You can do it by casting but may lose information when the leftmost bits of the value being cast get cut off. Going from a long to an int the 32 most significant bits would get truncated. For example the largest long you can have is -9223372036854775807. This is
01111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 in binary. Casting this to an int would result in the left most 32 bits getting chopped so it would be: 11111111 11111111 11111111 11111111 or -1 in decimal.
run this to see it in action:
hope that helps
Dave
[This message has been edited by Dave Vick (edited June 26, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic