wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes strange thing about char. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "strange thing about char." Watch "strange thing about char." New topic
Author

strange thing about char.

Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
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

Joined: May 28, 2003
Posts: 1
Actually this is casting.
complier does explicit casting when using
c+=i;
this is equal to
c= char(c + i);
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Similar vquestion has been posted before. Here's the url
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270

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


Meng Yi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: strange thing about char.