Java 检查变量是 Long 还是 Double
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4247849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:00:16 来源:igfitidea点击:
Checking if variable is Long or Double
提问by Gondim
Is there any way to check if the varible is Double or Long before doing anything with it? Sometimes the Objects are Double and some they are Long. How can I check it before using it?
有没有办法在对变量做任何事情之前检查它是 Double 还是 Long ?有时对象是双重的,有些对象是长的。使用前如何检查?
采纳答案by ColinD
if (obj instanceof Double) ...
回答by wkl
You could use instanceof
.
你可以使用instanceof
.
public void someMethod(Object o)
{
if ( o instanceof Long )
{
// do stuff
}
else if ( o instanceof Double )
{
// do stuff
}
}