【发布时间】:2011-12-21 16:49:14
【问题描述】:
让我们考虑以下 Java 中的简单表达式。
char c='A';
int i=c+1;
System.out.println("i = "+i);
这在 Java 中完全有效,并返回 66,即 c+1 的字符 (Unicode) 的对应值。
String temp="";
temp+=c;
System.out.println("temp = "+temp);
这在 Java 中太有效了,String 类型变量 temp 自动接受 char 类型的 c 并在控制台上生成 temp=A。
以下所有语句在 Java 中也令人惊讶地有效!
Integer intType=new Integer(c);
System.out.println("temp = "+intType);
Double doubleType=new Double(c);
System.out.println("temp = "+doubleType);
Float floatType=new Float(c);
System.out.println("temp = "+floatType);
BigDecimal decimalType=new BigDecimal(c);
System.out.println("temp = "+decimalType);
虽然 c 是 char 的一种类型,但可以在各自的构造函数中毫无错误地提供它,并且所有上述语句都被视为有效语句。它们分别产生以下输出。
temp = 65
temp = 65.0
temp = 65.0
temp = 65
在这种情况下,Java 中可用的 char 类型的内部行为是什么?
【问题讨论】: