This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assinging Compile time long constant to byte fails

 
Harish Kommaraju
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harish Kommaraju
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    Prashant R Kataria
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic