File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Cattle Drive and the fly likes Getting from long to int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Getting from long to int" Watch "Getting from long to int" New topic

Getting from long to int

Geoffrey Dagley

Joined: Feb 19, 1999
Posts: 2
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?
paul wheaton

Joined: Dec 14, 1998
Posts: 20919

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).

permaculture Wood Burning Stoves 2.0 - 4-DVD set
I agree. Here's the link:
subject: Getting from long to int
It's not a secret anymore!