Java 我创建了一个整数实例,但它给了我一个长实例
最近,我遇到了Java的一个陷阱,代码如下:Java 我创建了一个整数实例,但它给了我一个长实例,java,Java,最近,我遇到了Java的一个陷阱,代码如下: Number value = new Integer(10); value = value instanceof Long ? new Long(-value.longValue()) : new Integer(-value.intValue()); System.out.println(value instanceof Integer); System.out.println(value.getClass());
Number value = new Integer(10);
value = value instanceof Long ? new Long(-value.longValue()) : new Integer(-value.intValue());
System.out.println(value instanceof Integer);
System.out.println(value.getClass());
我认为结果是:
正确类java.lang.Integer 但是,我错了,正确的输出正好相反: 假
类java.lang.Long 我不明白为什么。因此,我使用
javap-c
反汇编类文件,以下两行(说明)对我来说很奇怪:
50: i2l
51: invokestatic #10 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
为什么??我想要对象值
的相同实例,为什么它会给我不同的值
非常感谢您的帮助。编译器需要格式化您的条件语句,以返回单个类型,而不是多个类型:
value = value instanceof Long ? new Long(-value.longValue()) : new Integer(-value.intValue());
三元运算符需要决定强制转换两种可能的返回类型之一,以便返回类型一致
我发现用与编写方法相同的方法来理解这一点最简单:
public Long getNumber(int n) {
if (value instanceof Long) {
return new Long(-value.longValue());
} else {
return new Integer(-value.intValue()); // Not consistent with return type
}
}
您需要在此实例中指定一致的返回类型。如果您将它们全部返回为Number
,则结果应该是ok:
public Number getNumber(int n) {
if (value instanceof Long) {
return (Number) new Long(-value.longValue());
} else {
return (Number) new Integer(-value.intValue());
}
}
同样地:
value = value instanceof Long ? (Number) new Long(-value.longValue()) : (Number) new Integer(-value.intValue());
为您提供所需的输出
概述了Java三元运算符如何决定这些转换的规则
它们基于信息保存的逻辑:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
只是为了给这些规则背后的逻辑提供一些背景,这样它们就不会显得那么武断了
Double
以最具体的方式作为先例,即你可以想象将Double
切割成int
会丢失信息((int)0.555->0
),而不是将int
转换成Double
,不会丢失信息((Double)1->1.0
)
然后Float
,因为Float
的精度低于double
,但高于long
或int
接下来是long
,因为它是更精确的版本int
,最后它们被视为int
s
因此,由于
int
是最不特定的,它通常会转换为其他Number
格式。编译器需要格式化您的条件语句以返回单个类型,而不是多个类型:
value = value instanceof Long ? new Long(-value.longValue()) : new Integer(-value.intValue());
三元运算符需要决定强制转换两种可能的返回类型之一,以便返回类型一致
我发现用与编写方法相同的方法来理解这一点最简单:
public Long getNumber(int n) {
if (value instanceof Long) {
return new Long(-value.longValue());
} else {
return new Integer(-value.intValue()); // Not consistent with return type
}
}
您需要在此实例中指定一致的返回类型。如果您将它们全部返回为Number
,则结果应该是ok:
public Number getNumber(int n) {
if (value instanceof Long) {
return (Number) new Long(-value.longValue());
} else {
return (Number) new Integer(-value.intValue());
}
}
同样地:
value = value instanceof Long ? (Number) new Long(-value.longValue()) : (Number) new Integer(-value.intValue());
为您提供所需的输出
概述了Java三元运算符如何决定这些转换的规则
它们基于信息保存的逻辑:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
只是为了给这些规则背后的逻辑提供一些背景,这样它们就不会显得那么武断了
Double
以最具体的方式作为先例,即你可以想象将Double
切割成int
会丢失信息((int)0.555->0
),而不是将int
转换成Double
,不会丢失信息((Double)1->1.0
)
然后Float
,因为Float
的精度低于double
,但高于long
或int
接下来是long
,因为它是更精确的版本int
,最后它们被视为int
s
因此,由于
int
是最不特定的,它通常会转换为其他Number
格式。编译器需要格式化您的条件语句以返回单个类型,而不是多个类型:
value = value instanceof Long ? new Long(-value.longValue()) : new Integer(-value.intValue());
三元运算符需要决定强制转换两种可能的返回类型之一,以便返回类型一致
我发现用与编写方法相同的方法来理解这一点最简单:
public Long getNumber(int n) {
if (value instanceof Long) {
return new Long(-value.longValue());
} else {
return new Integer(-value.intValue()); // Not consistent with return type
}
}
您需要在此实例中指定一致的返回类型。如果您将它们全部返回为Number
,则结果应该是ok:
public Number getNumber(int n) {
if (value instanceof Long) {
return (Number) new Long(-value.longValue());
} else {
return (Number) new Integer(-value.intValue());
}
}
同样地:
value = value instanceof Long ? (Number) new Long(-value.longValue()) : (Number) new Integer(-value.intValue());
为您提供所需的输出
概述了Java三元运算符如何决定这些转换的规则
它们基于信息保存的逻辑:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
只是为了给这些规则背后的逻辑提供一些背景,这样它们就不会显得那么武断了
Double
以最具体的方式作为先例,即你可以想象将Double
切割成int
会丢失信息((int)0.555->0
),而不是将int
转换成Double
,不会丢失信息((Double)1->1.0
)
然后Float
,因为Float
的精度低于double
,但高于long
或int
接下来是long
,因为它是更精确的版本int
,最后它们被视为int
s
因此,由于
int
是最不特定的,它通常会转换为其他Number
格式。编译器需要格式化您的条件语句以返回单个类型,而不是多个类型:
value = value instanceof Long ? new Long(-value.longValue()) : new Integer(-value.intValue());
三元运算符需要决定强制转换两种可能的返回类型之一,以便返回类型一致
我发现用与编写方法相同的方法来理解这一点最简单:
public Long getNumber(int n) {
if (value instanceof Long) {
return new Long(-value.longValue());
} else {
return new Integer(-value.intValue()); // Not consistent with return type
}
}
您需要在此实例中指定一致的返回类型。如果您将它们全部返回为Number
,则结果应该是ok:
public Number getNumber(int n) {
if (value instanceof Long) {
return (Number) new Long(-value.longValue());
} else {
return (Number) new Integer(-value.intValue());
}
}
同样地:
value = value instanceof Long ? (Number) new Long(-value.longValue()) : (Number) new Integer(-value.intValue());
为您提供所需的输出
概述了Java三元运算符如何决定这些转换的规则
它们基于信息保存的逻辑:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.
只是为了给这些规则背后的逻辑提供一些背景,这样它们就不会显得那么武断了
Double
作为最具体的