Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in implict conversion

 
sakthi moorthy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
byte b = 1;
char c = 2;
c = b;//not allowed

but if i make
final byte b=1;
then it works reason?
 
Panseer Kaur
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason it cannot covert it without it being final is because your byte variable could be negative, which is out of bounds for a char. By making it final you are stating that "a" 100% for sure will be positive.

If you change it to

final byte b = -104;
char c = 2;
c = b;

It will still throw the error because it needs to know you are giving it a positive number.
[ December 10, 2007: Message edited by: Panseer Kaur ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic