File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

implicit casting of final variables

 
Jared Cope
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
chowdary Thammineedi
Ranch Hand
Posts: 126
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
kapil munjal
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Akshatha Nayak
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
r final variables the only COMPILER TIME CONSTANT.. ??
 
Stefan Guilhen
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Robert Chisholm
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic