简体   繁体   English

java.lang.Integer 不能转换为 java.lang.Long

[英]java.lang.Integer cannot be cast to java.lang.Long

I'm supposed to receive long integer in my web service.我应该在我的 web 服务中收到长 integer 。

long ipInt = (long) obj.get("ipInt");

When I test my program and put ipInt value = 2886872928, it give me success.当我测试我的程序并输入 ipInt 值 = 2886872928 时,它给了我成功。 However, when I test my program and put ipInt value = 167844168, it give me error:但是,当我测试我的程序并将 ipInt 值 = 167844168 时,它给了我错误:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

The error is point to the above code.错误是指向上面的代码。

FYI, my data is in JSON format:仅供参考,我的数据采用 JSON 格式:

{
    "uuID": "user001",
    "ipInt": 16744168,
    "latiTude": 0,
    "longiTude": 0,
}

Is there any suggestion so that I can ensure my code able to receive both ipInteger value?有什么建议可以确保我的代码能够同时接收 ipInteger 值吗?

Both Integer and Long are subclasses of Number , so I suspect you can use: IntegerLong都是Number的子类,所以我怀疑你可以使用:

long ipInt = ((Number) obj.get("ipInt")).longValue();

That should work whether the value returned by obj.get("ipInt") is an Integer reference or a Long reference.无论obj.get("ipInt")返回的值是Integer引用还是Long引用,这都应该有效。 It has the downside that it will also silently continue if ipInt has been specified as a floating point number (eg "ipInt": 1.5 ) in the JSON, where you might want to throw an exception instead.它的缺点是,如果在ipInt指定为浮点数(例如"ipInt": 1.5 ),它也会静默继续,您可能希望在其中抛出异常。

You could use instanceof instead to check for Long and Integer specifically, but it would be pretty ugly.可以使用instanceof来专门检查LongInteger ,但这会非常难看。

We don't know what obj.get() returns so it's hard to say precisely, but when I use such methods that return Number subclasses, I find it safer to cast it to Number and call the appropriate xxxValue() , rather than letting the auto-unboxing throw the ClassCastException :我们不知道obj.get()返回什么,所以很难准确地说,但是当我使用返回Number子类的此类方法时,我发现将其强制转换为Number并调用适当的xxxValue()比让自动拆箱抛出ClassCastException

long ipInt = ((Number)obj.get("ipInt")).longValue();

That way, you're doing explicit unboxing to a long , and are able to cope with data that could include a .这样,您就可以对long进行显式拆箱,并且能够处理可能包含. , which would return a Float or Double instead. , 这将返回一个FloatDouble

You mention the current approach works when you provide a value outside the range of integer, but fails when you are within the integer range.您提到当前方法在您提供 integer 范围之外的值时有效,但在 integer 范围内时失败。 That is an odd behavior for an API, because it seems you need to check the return type yourself.对于 API 来说,这是一种奇怪的行为,因为您似乎需要自己检查返回类型。 You can do that.可以这样做。 The usual way is with instanceof .通常的方法是使用instanceof Something like,就像是,

long ipInt;
Object o = obj.get("ipInt");
if (o instanceof Integer) {
    ipInt = ((Integer) o).intValue();
} else if (o instanceof Long) {
    ipInt = ((Long) o).longValue();
}
Long.valueOf(jo.get("ipInt").toString());

Is ok.没关系。

public static void main(String[] args) {
    JSONObject jo = JSON.parseObject(
        "{    \"uuID\": \"user001\",    \"ipInt\": 16744168,    \"latiTude\": 0,    \"longiTude\": 0}");
    System.out.println(jo);
    long sellerId1 =  Long.valueOf(jo.get("ipInt").toString());
    //Long sellerId1 = (long)jo.get("ipInt");
    System.out.println(sellerId1);
}

in kotlin I simply use this:在 kotlin 我只是使用这个:

val myInt: Int = 10
val myLong = myInt.toLong()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将java.lang.Long强制转换为java.lang.Integer - java.lang.Long cannot be cast to java.lang.Integer java.lang.ClassCastException:java.lang.Long 不能在 java 1.6 中转换为 java.lang.Integer - java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer in java 1.6 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long - java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long 无法将java.lang.Long强制转换为java.lang.Integer JPA GAE和枚举 - java.lang.Long cannot be cast to java.lang.Integer JPA GAE and enumeration fastjson java.lang.Integer 不能转换为 java.lang.Long - fastjson java.lang.Integer cannot be cast to java.lang.Long ClassCastException java.lang.Integer 无法在获取共享首选项值时强制转换为 java.lang.Long - ClassCastException java.lang.Integer cannot be cast to java.lang.Long on getting the Shared Preference value 将方法传递给Controller:java.lang.Long不能强制转换为java.lang.Integer - Passing methods to Controller: java.lang.Long cannot be cast to java.lang.Integer Java 中的 JSON 加载:java.lang.ClassCastException:java.lang.Long 不能转换为 java.lang.Integer - JSON Loading in Java: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer SDN4 java.lang.ClassCastException:使用AttributeConverter时无法将java.lang.Integer强制转换为java.lang.Long - SDN4 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long when using AttributeConverter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM