• 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

Why a final long variable within a int range can not be assigned to int variable

 
Ranch Hand
Posts: 61
Firefox Browser Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi friends .
Can anyone please explain me why linr number 7 requires a cast.I know whenever you assign a long variable to an int variable an explicit casr is required.But here our long variable l is a final variable and thus compiler knows its value at compile time and it is 90 which is not going to change further in program and 90 can be easily placed in an int container.So why compiler is complaining and giving a compile time error?
 
author & internet detective
Posts: 41871
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler doesn't know everything that you do. In this case, it doesn't look at the value of the long to see that it is in range. This also forces you to be explicit about what you are doing as a warning to future programmers.
 
Enthuware Software Support
Posts: 4817
52
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishek KumarSoni wrote:
Can anyone please explain me why linr number 7 requires a cast.I know whenever you assign a long variable to an int variable an explicit casr is required.But here our long variable l is a final variable and thus compiler knows its value at compile time and it is 90 which is not going to change further in program and 90 can be easily placed in an int container.So why compiler is complaining and giving a compile time error?


Because the language designers did not make it acceptable. As per Section 5.2 of JLS, such implicit narrowing can happen only if the expression is a constant of type byte, short, char, or int and the type of the variable is byte, short, or char.

So, no long. Even if it is a constant (in which case the compiler does know its value)

If you want to have fun, try the following code:
 
Abhishek KumarSoni
Ranch Hand
Posts: 61
Firefox Browser Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:

Abhishek KumarSoni wrote:
Can anyone please explain me why linr number 7 requires a cast.I know whenever you assign a long variable to an int variable an explicit casr is required.But here our long variable l is a final variable and thus compiler knows its value at compile time and it is 90 which is not going to change further in program and 90 can be easily placed in an int container.So why compiler is complaining and giving a compile time error?


Because the language designers did not make it acceptable. As per Section 5.2 of JLS, such implicit narrowing can happen only if the expression is a constant of type byte, short, char, or int and the type of the variable is byte, short, or char.

So, no long. Even if it is a constant (in which case the compiler does know its value)

If you want to have fun, try the following code:


Hey why this is happening with final it is showing unreachable statement.Would you please explain a little more about this scenerio?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
unreachable statement. because the instance variable (l) marked final so you can not reassign new value OK !!
in while statement the expression (l<10) is false and never change it to true so the cod inner while does not execute never .
but if you initialization any number less than (10) the code will be execute .
 
Abhishek KumarSoni
Ranch Hand
Posts: 61
Firefox Browser Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohammad jordan wrote:unreachable statement. because the instance variable (l) marked final so you can not reassign new value OK !!
in while statement the expression (l<10) is false and never change it to true so the cod inner while does not execute never .
but if you initialization any number less than (10) the code will be execute .


It is ok and I also understand that compilation is fail due to unreachable statement but my question is here compiler knows it very well that long is final and can never be less than 10 its ok and expected behaviour
but if i write

Here why I can not assign a final long variable l which is 90 to an int container without an explicit cast. 90 is within int range and it is final and it can be placed in int container without any loss then why compiler not allow this.
 
mohammad jordan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because the long 64 bit ant int 32 bit , when you assign any long variable to any primitive less than long must explicit cast .
the value 90 it OK !!that in the int range, but compiler don't know about this.

try this.


is legal and double Greater than long so not compilation error here it implicit casting like this


note : when using this illegal code


advance note : Imagine compiler allowed and success compilation time in run time Exception (Class Cast Exception).but java Shifting this Error from run time to compile time Error.


 
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

Abhishek KumarSoni wrote:
It is ok and I also understand that compilation is fail due to unreachable statement but my question is here compiler knows it very well that long is final and can never be less than 10 its ok and expected behaviour



First, the compiler only knows about compile time constants -- and yes, in the examples shown, they were all compile time constants. However, it take more than the final keyword to make a variable a compile time constant variable. I know I am being anal regarding this, but I need to point out that we are talking about compile time constants -- and not just final variable. The conversation so far, seems to imply that only final is needed.

Abhishek KumarSoni wrote:
but if i write

Here why I can not assign a final long variable l which is 90 to an int container without an explicit cast. 90 is within int range and it is final and it can be placed in int container without any loss then why compiler not allow this.



As already mentioned by Paul... that is how it is defined. Yes, you can argue that it is a compile time constant variable. And yes, the compiler should know that it is in range of an int, and hence, should allow the assignment. However, section 5.2 of the JLS states that it is not allowed to do the assignment for longs. Perhaps, in the future, the JLS will be changed to allow the assignment, and hence, it will work for future versions of Java. Currently, however, it is simply defined to not allow the assignment. Sorry.

Henry
 
no wonder he is so sad, he hasn't seen this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic