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
posted
0
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