【发布时间】:2014-10-01 12:54:38
【问题描述】:
所以我有这个代码
char [] a = {'a','b','c'};
char c = 'a' + 'b'; //works
char c2 = 98 + 97; //works
char c3 = a[0] + a[1]; //compile time error
所以它们都是相同的功能,但是在获取和使用数组值时,它会给我一个编译时错误。这是什么原因??
The result of the additive operator applied two char operands is an int.
那我为什么可以这样做?
char c2 = (int)((int)98 + (int)97);
【问题讨论】:
-
@ScaryWombat 上面有评论。我认为他是在说为什么它们都可以工作,但数组部分却不行
-
@ScaryWombat 需要
char找到int -
@ScaryWombat 线程“main”java.lang.RuntimeException 异常:无法编译的源代码 - 不兼容的类型:从 int 到 char 的可能有损转换
-
@Rod_Algonquin 我的眼睛一定很糟糕——评论在哪里?
-
@ScaryWombat
char c3 = a[0] + a[1]; //compile time error