aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is this an implicit cast? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is this an implicit cast?" Watch "Is this an implicit cast?" New topic
Author

Is this an implicit cast?

Adrian Stent
Greenhorn

Joined: Nov 19, 2004
Posts: 4
Why doesnt 'g' require a cast back down to byte after arithmetic is performed? The variable 'h' requires an explicit cast as the variables are promoted to int for the arithmetic.


Hope someone can explain this to me..
Roopesh Gulecha
Ranch Hand

Joined: Aug 09, 2005
Posts: 32
Adrian,
In the example g doesn't require a cast because what is assigned to it is
1. compile time constant integer (value 13)..(no variables involved), and
2. the constant fits in the type byte.
So the compiler is happy...

whereas variable h is assigned values of another variable(s), which is not considered compile time constant (unless the variable is a final constant)..to the compiler it means that the value of the variable may change while the program is running, so it flags an error.

Hope this helps.

Roopesh.
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
Do Try This ,it will help,we know final variable act as constant.


Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
thank U Mr. roopesh
The Information given by U was Basi information. But Worth forgetting


######33
Agrah upadhyay
3rd Year
Bioinformatics
Adrian Stent
Greenhorn

Joined: Nov 19, 2004
Posts: 4
Thanks a lot, that makes sense it being a compile time constant within the range of the variable. The code proves it as well.

Cheers.
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
I Dont Think We Should Comment on any post,what i know that mr Roopesh provided us with a very simple and good and informative logic,and for every posting one should be appreciated,so Thanks mr Roopesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this an implicit cast?