Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strange thing about char.

 
Yuan Ye
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Supposing we have following defination:
char c = 10;
int i = 20;
I understand why c=c+i can't be compiled. But why c+=i can be compiled? Please help.
Thanks
 
Fahad Fahad
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually this is casting.
complier does explicit casting when using
c+=i;
this is equal to
c= char(c + i);
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Similar vquestion has been posted before. Here's the url
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

c= char(c + i);

should be c=(char)(c+i);
For expressions like opt1+=opt2; they can always interpreted as:
opt1=(T)(opt1+opt2);
where T is the type of opt1. This also applies to operators such as : -+, *=, /=, etc
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic