File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes Say4b:  Typecasting Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Reply Bookmark "Say4b:  Typecasting" Watch "Say4b:  Typecasting" New topic
Author

Say4b: Typecasting

David Junta
Ranch Hand

Joined: Dec 10, 2000
Posts: 86
If I have a long variable and I want to get an int value from it, I think I typecast it like this:
long argNum = Long.parseLong( whole );
int billNum = ( (int)argNum / 1000000000 );
But what happens if you cast argNum as an integer and argNum is larger than the largest number that Integer can handle? Will it still get cast?
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
A cast will always work, but if the number is too big, it will get cut off. By casting something you are telling the compiler, I know that this is dangerous because I am going from a larger value down to a smaller value, but trust me, I know what I am doing.
For example if you have this:

The output will be -2
This has to do with the bit representation. The 32 bits for the int 254 will be cut down to 8 bits for a byte. The 24 high level bits will be lost, and what remains is the bit pattern for -2.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Say4b: Typecasting
 
Similar Threads
Comparable again
Parsing an Int
Widening & Boxing
MAX_VALUE and Casting
int VS Integer