• 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

implicit casting of final variables

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've come across this particularly evil example of code:



So what will happen here? We all know that an int can't be assigned to a byte without an explicit cast, so obviously the code won't compile. However, to my surprise it does! However, if you take out the final modifier for the i variable, the code won't compile. Moreover, if i gets assigned 200 (something larger than the range of a byte) and is final then it won't compile.

I'm just wondering what is happening behind the scenes. Is the compiler reclassifying i as a byte variable since it knows the value at compile time and knows that it can't change during runtime with the final modifier?
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jared

The compiler will automatically narrow down the assignment.
Dan Chisholm on his website explains this very well. Visit his site and your doubt's about automatic conversions will be DONE forever.

The rule is that in an assignment statement if the right hand side of the assignment is a COMPILE TIME CONSTANT of type byte,char,shot or int then it will be automatically converted(narrowed) to the reference type of the left hand side.

Chowdary
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think, COMPILE TIME CONSTANTS means final variables.

well, I think I am right about this, but if I am wrong then please correct me.

Thanks
Kaps
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
r final variables the only COMPILER TIME CONSTANT.. ??
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Not only final variables are compiler time constants. You can express a compiler time constant as a literal too. For example:



Both case clauses are valid, because the arguments are compiler time constants, one represented by a literal and one by a final variable. Notice that the following does not compile:



The preceding code does not compile, because althoght x is a final variable it is not a compiler time constant, since its value will only be determined at runtime when the method amethod is invoked.

Hope that helps.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a corollary to the above post, this won't compile either. Although it's a bit off-topic, I notice it in a few mock exams.

 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Robert,

could you tell me why there will be no implicit narrowing cast to int in your example.

Please explain this concept.

Thanks
Kaps
 
Stefan Guilhen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rules for variable assignments do not apply to arguments passed to methods. I can remember that Dan's mock has an explanation about this... it seem the developers of Java chose not to perform an implicit cast in values passed as arguments because it would be dificult to determine which method should be called in case of overloading. Imagine this scenario:



As you can see, problems may arise here if the implicit casting is performed in arguments to methos. The value of x fits in an int, but its type is a long.... what should the compiler do to determine the method the programmer wants to be called?

Stefan.
[ August 20, 2004: Message edited by: Stefan Guilhen ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic