3

I have a question regarding the conversion from int into long in java. Why for floats there is no problem:

float f = (float)45.45;//compiles no issue.
float f = 45.45f; //compiles no issue.

However for the long type it seems to be a problem:

    long l = (long)12213213213;
    //with L or l at the end it will work though. 
    long l = (long)12213213213L;

It seems that once the compiler notify an error due to an out-of-range issue it blocks there without checking for any possible casting that the programmer might have planned.

What's your take on it? Why is it like that there is any particular reason?

Thanks in advance.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Rollerball
  • 12,618
  • 23
  • 92
  • 161

2 Answers2

7

However for the long type it seems to be a problem:

That's because 12213213213 without L is an int, and not long. And since that value is outside the range of the int, so you get an error.

Try something like:-

System.out.println(Integer.MAX_VALUE);
System.out.println(12213213213L);

you will understand better.

As far as the case of float is concerned: -

float f = (float)45.45;

the value 45.45 is a double value, and fits the size of double. So, the compiler will have not problem with that, and then it will perform a cast to float.

It seems that once the compiler notify an error due to an out-of-range issue it blocks there without checking for any possible casting that the programmer might have planned.

Exactly. The compiler first checks for a valid value first, here int, only then it can move further with the cast operation.

So, basically in case of long and int: -

long l = (long)12213213213;

you are not getting error because of the cast operation, rather because your numeric literal is not representable as int. So, compiler didn't get the valid value there, to perform a cast operation.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • I understand that, However my question aimed to understand why the casting (long)12213213213; does not get taken in consideration in this case whereas in float f = (float)45.45; (note that without the casting it gives an error) works normally. – Rollerball Jan 31 '13 at 18:49
  • 1
    @Rollerball.. The error is not because of the casting. The compiler don't get chance to reach there. It stops only when noticing `12213213213`, and says, hey wait, this number can't be represented as an `int`. So, it gives you an error. And this is implied by the fact that, a number without an `L` at the end is an `int`. So, `4` is an `int`, but `4L` is a `long`. – Rohit Jain Jan 31 '13 at 18:50
  • @ everybody reading this comment. I thoroughly understand the point of the error. However does anybody know why the compiler stops there without checking for casting? Taking into account that if the cast had been seen by the compiler it'd have worked. – Rollerball Jan 31 '13 at 18:54
  • @Rollerball.. Ok, let's take it this way. For casting a value to a type, compiler needs a valid value first. Then only it can check whether that value is castable to the type or not. Now, since it is not even getting the valid value for the `int literal`, how is it going to move further with the cast? – Rohit Jain Jan 31 '13 at 18:57
  • This may help http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2 – kosa Jan 31 '13 at 19:04
4

Java doesn't consider what you do with a value only what it is. For example if you have a long value which is too large it doesn't matter that you assign it to a double, the long value is checked as valid first.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130