1

Is it safe to cast a long to an int in Java?

For some reason, the following code , I get '-1' as anIntVar. And I have checked 'aLongVar' is not an -1.

public static final long CONST = 1000 * 62;

long aLongVar
int anIntVar =  (int)(aLongVar/ CONST);
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
michael
  • 106,540
  • 116
  • 246
  • 346
  • You mean aside from the fact that a long holds more information that would be lost in the conversion to int? – JohnFx Jun 13 '11 at 18:13
  • possible duplicate - http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java – mre Jun 13 '11 at 18:13
  • This code does not even compile. – jzd Jun 13 '11 at 18:13
  • 2
    Well what *is* the value of `aLongVar`? – Oliver Charlesworth Jun 13 '11 at 18:14
  • Please post working code examples.... – joekarl Jun 13 '11 at 18:14
  • 1
    I think `aLongVar` is `-62000` thus equaling `-1` ! :P – Pangolin Jun 13 '11 at 18:15
  • Or maybe it was 532575944642000. Then divide by 62000 gives 8589934591, which in hex is 1FFFFFFFF. Cast to an int and you have -1. – Jay Jun 13 '11 at 18:19
  • What is the value of aLongVar ? We can't assign a default value of 0L to it, since it will result the o/p as 0. – Saurabh Gokhale Jun 13 '11 at 18:24
  • @Jay, hahahaha, but you're not casting from hex to an int? You are casting from 8589934591 to an int. ;-) – Pangolin Jun 13 '11 at 18:25
  • @Nideo: Decimal and hex are just display formats. The underlying number is the same either way. There is no such thing as a Java or a C "hex integer" as opposed to a "decimal integer". – Jay Jun 13 '11 at 20:47
  • @Jay, I know that - but try `long x = 8589934591;` and say `x/62000` - that would not equal `-1` . – Pangolin Jun 14 '11 at 05:17
  • @Nideo: Wait, I said to cast the 8589934591, not divide again. Try this: long x=532575944642000L; long q=x/62000L; int n=(int) q; Then q will be 8589934591. n will be -1. – Jay Jun 14 '11 at 14:59
  • @Nideo: Or try simply this: int n=(int) 4294967338L; System.out.println(n); It will display "42". – Jay Jun 14 '11 at 15:12
  • Cool! Did not know :) I only work with int's and double's ... sometimes float - but I have no working-knowledge of long. – Pangolin Jun 14 '11 at 15:30

5 Answers5

2

If it was safe it wouldn't require an explicit cast.

ninjalj
  • 42,493
  • 9
  • 106
  • 148
2

No, its not safe. Don't do it. Because, A Long / long has 64 bit and can hold much more data than an Integer / int has 32 bit only. Doing the conversion will result in loss of information.

If at all you wish to do it, do like this

Use the .intValue() to convert the Long value to an int value.

Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
1

A long is 64 bits, and an int is 32 bits, so no, it's not safe.

John
  • 15,990
  • 10
  • 70
  • 110
  • What do you mean by 'not safe'? The fact that the last few digits will be lost? – Pangolin Jun 13 '11 at 18:16
  • @Nideo: By that I mean that in general it's a bad idea to do that, because of what you said, or because of the fact that if the `long` contains a value that's larger than the maximum value for `int`, there could be garbage after the cast. – John Jun 13 '11 at 18:19
1

Try

int anIntVar = Math.rint(aLongVar);
Stormbreaker
  • 133
  • 1
  • 5
0

You can cast a long to an int and get meaningful results if the value in the long will fit in an int. If not, you will get high bits truncated. In your example you don't show what was in the numerator before you did the division, but -1 is certainly not an impossible value given the right inputs.

Is it legal? Yes. Is it safe? No, not unless you do range checks, or are confident that you know your inputs well enough.

Jay
  • 26,876
  • 10
  • 61
  • 112