| Author |
Litteral Conversion Auto Cast
|
Rafi Fareen
Ranch Hand
Joined: Aug 28, 2009
Posts: 75
|
|
hi guys,
anyone can explain that:
i don't need to put caste for the above, because the compiler does that for me.
what about here ...
by why do i get compiler error when i assign b+c to c?
does that mean compiler puts caste automatically/implicitly only for literal assignments, not for expression assignments?
regards,
Rafi
|
 |
Shailesh Phatak
Ranch Hand
Joined: Aug 27, 2009
Posts: 63
|
|
what if the value of c exceeds 127 ?
Thats why compiler give an error during compilation because once it passes the compilation part Runtime cant do anything and this could be the disastrous situation.
|
See Dreams With Open Eyes And Make It Real
|
 |
Harpreet Singh janda
Ranch Hand
Joined: Jan 14, 2010
Posts: 317
|
|
While assiging a value, You don't have to put a cast if the value you are assigning in within the limit of variable like
But you have to put a cast if limit exceeds the limit
While eveluating the expresion the outcome of the expression is always auto promoted to wider type of the element being used in the expression.
and byte, short are always promoted to int.
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Rafi Fareen wrote:hi guys,
anyone can explain that:
i don't need to put caste for the above, because the compiler does that for me.
what about here ...
by why do i get compiler error when i assign b+c to c?
does that mean compiler puts caste automatically/implicitly only for literal assignments, not for expression assignments?
regards,
Rafi
This is called as Arithmetic promotion for binary operators....
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Jehaan Butt
Ranch Hand
Joined: Feb 05, 2009
Posts: 41
|
|
|
The bedrock here is this - whenever any arithmetic operation involving two different variables is carried out, the answer is always at least an int, even if the two operands are bytes or shorts.
|
SCJP 6
|
 |
Mark Moge
Ranch Hand
Joined: Mar 14, 2009
Posts: 96
|
|
in addition
You can do sth like this
but you cannot do
|
SCJP6 | http://programmerutilities.blogspot.com/
|
 |
 |
|
|
subject: Litteral Conversion Auto Cast
|
|
|