| Author |
byte question
|
jaman tai
Ranch Hand
Joined: Sep 26, 2005
Posts: 37
|
|
byte b = 0; b += 1; //1 no error b = b + 1; //2 error why //1 has no error?
|
 |
Srinivasa Raghavan
Ranch Hand
Joined: Sep 28, 2004
Posts: 1228
|
|
Every arithmetic operation evaluates atleast to an integer ( primitive int ). You have to do a explicit type cast in second line, but += does this implicitly. HTH
|
Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
|
 |
jaman tai
Ranch Hand
Joined: Sep 26, 2005
Posts: 37
|
|
|
oh, thanks Srinivasa!
|
 |
Andreas Sandberg
Ranch Hand
Joined: Sep 14, 2005
Posts: 31
|
|
Don't forget, there are also rules about automatic implcit narrowing promotion. Fore example. byte b = 3; //Ok, int i = 3; byte c = i; //Not ok, not resolved at compile time. So for implicit automatic promotion the following apply: a) The operand to the right of the assignment operator must be a byte, char, short, or int. b) Operand on the right must be in the range of the operator on the left c) Operator on the left must be a byte, short, or char. Anything else required an implicit cast. int x = 3L; //Compile error, need a cast int x = (int) 3L; //OK.
|
"When the compilers not happy ain't nobody happy"
|
 |
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
|
|
Hi, int i=3; byte c = i;
a) The operand to the right of the assignment operator must be a byte, char, short, or int. b) Operand on the right must be in the range of the operator on the left c) Operator on the left must be a byte, short, or char. Anything else required an implicit cast.
The above example...satisfies all three conditions... Then why the compilation error.. You have missed the keyword "final" put int i as final int i=3..and then try..
|
 |
Parthiban Murugesan
Greenhorn
Joined: Oct 04, 2005
Posts: 2
|
|
Originally posted by A Kumar: Hi, int i=3; byte c = i; The above example...satisfies all three conditions... Then why the compilation error.. You have missed the keyword "final" put int i as final int i=3..and then try..
|
 |
Parthiban Murugesan
Greenhorn
Joined: Oct 04, 2005
Posts: 2
|
|
Originally posted by Parthiban Murugesan: [QB][/QB]
Hi the solution adding "final" may not work if final int b =130 :-) as this would ask for type casting.
|
 |
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
|
|
Hi,
Hi the solution adding "final" may not work if final int b =130 :-) as this would ask for type casting.
b) Operand on the right must be in the range of the operator on the left It is not in the range so...
|
 |
 |
|
|
subject: byte question
|
|
|