• Post Reply Bookmark Topic Watch Topic
  • New Topic

byte question  RSS feed

 
jaman tai
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte b = 0;
b += 1; //1 no error
b = b + 1; //2 error

why //1 has no error?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jaman tai
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, thanks Srinivasa!
 
Andreas Sandberg
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!