• 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

Getting from long to int

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question about the extra credit on the second problem. The number 999,999,999,999 is obviously too large to be an integer. So instead I have used a long. The problem comes when I want to convert the long to an int. I have not seen a way to do this. However, I saw something about Integer and int. Is there a similar long and Long adn would there be a method for Long to convert it an int?
 
Trailboss
Posts: 23773
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if this will answer your question, but here goes:
If you want to convert a long to an int and you are sure that the value in the long will fit in the int, do it like this:
long myLong = 500 ;
int i = (int) myLong ;
This is called "casting". The only problem comes from when the value in the long is too big for the int. In that case, I think an exception is thrown (in C and C++ it just "makes the best of it" copies the lowest order bytes, so you end up with a modulo of MAXINT).

reply
    Bookmark Topic Watch Topic
  • New Topic