方法调用中的 Null、原始数据值和包装器 class
Null, primitive data value and wrapper class in method call
我有以下代码片段
public class Sample {
public static void main(String[] args) {
test(1);//fine & output - int
test(1L);//fine & output - long
//test(null); fine & output - Integer
test(12233333333);//error
}
static void test(int a){
System.out.println("int");
}
static void test(Integer a){
System.out.println("Integer");
}
static void test(long a){
System.out.println("long");
}
static void test(Long a){
System.out.println("Long");
}
static void test(List<Integer> a){
System.out.println("List");
}
}
我的问题是,
为什么数字值总是引用 int(原始类型)而 null 引用重载中最具体的方法?
为什么我们需要隐含地说 1L 为什么如果你给大数字(1233333333333)它不取长值?
你能解释一下吗?
1233333333333 是一个 int 文字,因此您可以使用它,但您必须将其转换为 long,在这种情况下,编译器将进行转换。它不起作用,因为它太大而不能成为 int。
编译器不能假设它很长的原因是因为它们与 123 是一个 int 不一致。
Why number value always refer int(primitive type) but null refer the
most specific method among the overloads?
因为默认情况下编译器指的是没有后缀 l
或 L
(for long
) 的数字的 32 位整数运算.
Why we need to say implicitly 1L why it not taking long
value if u
give big number(1233333333333)? Can u some one explain those?
正如我上面所说,它默认是指整数运算,对于这么大的数字,它超出了 Integer
的范围,因为这个数字将被视为没有后缀 [=10= 的整数] 或 L
所以它会引用以 int
作为参数的方法,但会出现值超出范围的错误。
看看JLS怎么说..
JLS 4.2
If an integer operator other than a shift operator has at least one
operand of type long, then the operation is carried out using 64-bit
precision, and the result of the numerical operator is of type long.
If the other operand is not long, it is first widened (§5.1.5) to type
long by numeric promotion (§5.6).
Otherwise, the operation is carried out using 32-bit precision, and
the result of the numerical operator is of type int. If either operand
is not an int, it is first widened to type int by numeric promotion.
我有以下代码片段
public class Sample {
public static void main(String[] args) {
test(1);//fine & output - int
test(1L);//fine & output - long
//test(null); fine & output - Integer
test(12233333333);//error
}
static void test(int a){
System.out.println("int");
}
static void test(Integer a){
System.out.println("Integer");
}
static void test(long a){
System.out.println("long");
}
static void test(Long a){
System.out.println("Long");
}
static void test(List<Integer> a){
System.out.println("List");
}
}
我的问题是,
为什么数字值总是引用 int(原始类型)而 null 引用重载中最具体的方法?
为什么我们需要隐含地说 1L 为什么如果你给大数字(1233333333333)它不取长值? 你能解释一下吗?
1233333333333 是一个 int 文字,因此您可以使用它,但您必须将其转换为 long,在这种情况下,编译器将进行转换。它不起作用,因为它太大而不能成为 int。
编译器不能假设它很长的原因是因为它们与 123 是一个 int 不一致。
Why number value always refer int(primitive type) but null refer the most specific method among the overloads?
因为默认情况下编译器指的是没有后缀 l
或 L
(for long
) 的数字的 32 位整数运算.
Why we need to say implicitly 1L why it not taking
long
value if u give big number(1233333333333)? Can u some one explain those?
正如我上面所说,它默认是指整数运算,对于这么大的数字,它超出了 Integer
的范围,因为这个数字将被视为没有后缀 [=10= 的整数] 或 L
所以它会引用以 int
作为参数的方法,但会出现值超出范围的错误。
看看JLS怎么说..
JLS 4.2
If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion (§5.6).Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.