Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我创建了一个整数实例,但它给了我一个长实例_Java - Fatal编程技术网

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());

最近,我遇到了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());
我认为结果是:

正确
类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
作为最具体的