• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

byte question

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte b = 0;
b += 1; //1 no error
b = b + 1; //2 error

why //1 has no error?
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, thanks Srinivasa!
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..


 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
reply
    Bookmark Topic Watch Topic
  • New Topic