如何检查java中的值是否为整数? [英] How to check if the value is integer in java?
本文介绍了如何检查java中的值是否为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用restTemplate的一些API。 API返回一个类型为整数的键。
I'm using some API by restTemplate. The API returns a key whose type is integer.
但我不确定该值,所以我想检查密钥是否真的是整数。
我认为它可能是一个字符串。
But I'm not sure of that value, so I want to check whether the key is really an integer or not. I think it might be a string.
检查该值是否真的是整数的最佳方法是什么?
What is the best way of checking if the value is really integer?
已添加:
我的意思是某些API可能会返回如下所示的值。
{id:10}或{id:10}
added: I mean that some API might return value like below. {id : 10} or {id : "10"}
推荐答案
Object x = someApi();
if (x instanceof Integer)
请注意,如果 someApi()
返回类型整数
返回的唯一可能性是:
Note that if someApi()
returns type Integer
the only possibilities of something returned are:
- 一个
整数
-
null
- an
Integer
null
在这种情况下你可以:
if (x == null) {
// not an Integer
} else {
// yes an Integer
}
这篇关于如何检查java中的值是否为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文