Harish Kommaraju wrote:Why is that compile time constant of type int can be assigned to byte without any implicit cast, but we cannot assign compile time constant long to byte?
There are special rules for narrowing conversions of constant variables. The JLS states what types may be used the way you suggest and long isn't included. I have no explanation for this. Maybe it's somehow related to the fact that final fields are special when it comes to concurrency and long isn't aromic in that respect, but that's a long shot.
I think, this behaviour is because long and double are treated differently in java. They are 64bit and two registers will be required to store long/double constants. Since byte ... int constants are all stored in single registers, compiler should be able to compute & compare their value. However, long will require some special treatment to compute its value.
Moreover, if you know the java byte codes, there is no longtobyte conversion instruction, there is only longtoint conversion instruction.
Anyways, Even I am not aware of any exact reason or not satisfied with above explanation.
Are you using 32bit JVM? Could you execute same test in 64bit enviornment to see if it behaves differently.
"If you are going through hell, keep going!!!"
Joined: Nov 10, 2009
Thanks for the both of the responses.
Yes I am currently using 32 bit JVM. I will try to execute the code if I get hold of any such processor.
What difference will it make if you use a 64-bit computer? If Java is platform-independent, surely it will behave the same way on a 64-bit PC as a 32-bit machine. You will have to explore the Java™ Language Specification and see whether it tells you anything helpful.
Prashant R Kataria
Joined: Jul 23, 2007
Yes, I agree with you, it should not make any difference. I searched through java language specification and ideally long constant to byte should have worked. But, unfortunately, it did not.
I thought, only difference is long being 64 bit which will require two registers and int will require one. So I was thinking that on 64bit environment the size of the register would be 64bit and hence all types, byte to long, will reside in single register. This was just a wild thought/assumption and that's why I recommended one experiment to clear this point if it has anything to do with this theory.
Prashant R Kataria wrote:Yes, I agree with you, it should not make any difference. I searched through java language specification and ideally long constant to byte should have worked. But, unfortunately, it did not.
I guess you just need to know where to look. Check out section 5.2 of the JLS, about 3 or 4 paragraphs down.
Java Language Specification wrote:In addition, if the expression is a constant expression (§15.28) of type byte, short, char or int :
A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
You will see that there are no mention of long, float, or double.