Tag: 隐式

隐式转换为字符串 – toString和int +“”

为什么当我使用它: int a = 1; methodWithParamString(a + “”); a被强制转换为String,我不能在整数上使用toString()吗? int a = 1; methodWithParamString(a.toString()); 这不是: a+””作用类似于: a.toString() + “” ?

Java – 为什么char会被隐式地转换为byte(和short)原语,而不应该?

编译器的某些function让我感到困惑(使用Eclipse的Oracle JDK 1.7)。 所以我有这本书说char原语需要明确地转换为short和byte,这一切都有意义,因为数据类型的允许范围不重叠。 换句话说,下面的代码可以工作(但如果没有显式类型转换,则无法工作): char c = ‘&’; byte b = (byte)c; short s = (short)c; 正确打印b或s会显示数字38,这是Unicode中(&)的数字等效值。 这让我想到了我的实际问题。 为什么以下工作也一样? byte bc = ‘&’; short sc = ‘&’; System.out.println(bc); // Correctly displays number 38 on the console System.out.println(sc); // Correctly displays number 38 on the console 现在我肯定会理解以下内容(也适用): byte bt = (byte)’&’; System.out.println(bt); // Correctly displays […]