• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assinging Compile time long constant to byte fails

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic