This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is this an implicit cast?" Watch "Is this an implicit cast?" New topic

Is this an implicit cast?

Adrian Stent

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
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) 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.

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

Agrah upadhyay
3rd Year
Adrian Stent

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.

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:
subject: Is this an implicit cast?
Similar Threads
Doubt on Dan's Question
Implicit Narrowing and Casting
Primitive Assignments and Casting
Integer literals implicit casting
Round-up Question #13