【发布时间】:2014-06-26 07:25:23
【问题描述】:
为什么会出现编译时错误? 2 在编译时是常量,因此此处应允许缩小,因为 2 在字节范围内。
public class Test {
public static void main(String[] args) {
ForTest test=new ForTest();
test.sum(1, 2); //compile time error here
}
}
class ForTest
{
public int sum(int a,byte b)
{
System.out.println("method byte");
return a+b;
}
}
错误是: ForTest 类型中的 sum(int,byte) 方法不适用于参数 (int,int)。
编辑:我认为答案就在这里:http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.3 但我不明白 :(
【问题讨论】:
-
至少现在有字节文字可以缓解这个问题(我认为)。