Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iimplicit cast

 
Fox Hu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte b=127;
byte c=126;
byte d=b-c;
In the third line the compiler complains .
It seams that the result is casted to int implicitly when two number calculate, but why?
And are there any other cases do the implicit cast appears ?
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it is compiled!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
 
Paula Jiang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Kathy/Bert 's book, page 148-149: "the result of an expression involving anything int-sized or smaller is always an int. In other words, add two bytes together and you'll get an int."
So the compiler will complain that it can't convert int to byte, you need to explicit case to byte.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic