jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Assinging Compile time long constant to byte fails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Assinging Compile time long constant to byte fails " Watch "Assinging Compile time long constant to byte fails " New topic
Author

Assinging Compile time long constant to byte fails

Harish Kommaraju
Greenhorn

Joined: Nov 10, 2009
Posts: 13
Hi,

I am aware that if you are assigning int to a byte without any implicit cast, then the int should be a compile time constant and value should be in byte range.
i.e. the below code compile fines.


But if I try the same thing with long, it fails.


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?

Thanks,
Harish
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
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.
Prashant R Kataria
Greenhorn

Joined: Jul 23, 2007
Posts: 20
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.


-
Prashant Kataria
SCJP 5.0

"If you are going through hell, keep going!!!"
Harish Kommaraju
Greenhorn

Joined: Nov 10, 2009
Posts: 13
Thanks for the both of the responses.

Prashant,

Yes I am currently using 32 bit JVM. I will try to execute the code if I get hold of any such processor.

Thanks,
Harish
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
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
Greenhorn

Joined: Jul 23, 2007
Posts: 20
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18707
    
  40

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.

    Henry


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Prashant R Kataria
    Greenhorn

    Joined: Jul 23, 2007
    Posts: 20
    Oh I missed it, apologize for creating confusion.

    But question remained there as it is, why would they not allow long constant conversion to byte if it is in the range of byte.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Assinging Compile time long constant to byte fails