java的:布尔}这种布尔? [英] java: boolean instanceOf Boolean?
本文介绍了java的:布尔}这种布尔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有点困惑:我有一个函数,这需要一个对象作为参数。但是,如果我只是通过一种原始的,甚至识别出基本布尔布尔对象编译器不抱怨。为什么会这样?
公共字符串测试(对象的值)
{
如果((值的instanceof布尔)!)返回无效;
如果(((布尔)值).booleanValue()== true)而回是;
如果(((布尔)值).booleanValue()==假)重返不;
返回不知道;
}字符串结果=测试(真); //将导致是
解决方案
由于原始真正
将 <一个href=\"http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html\">Autoboxed到布尔
和这是一个对象
。
I'm a bit confused: I have a function, that takes an Object as argument. But the compiler does not complain if I just pass a primitive and even recognizes a boolean primitive as Boolean Object. Why is that so?
public String test(Object value)
{
if (! (value instanceof Boolean) ) return "invalid";
if (((Boolean) value).booleanValue() == true ) return "yes";
if (((Boolean) value).booleanValue() == false ) return "no";
return "dunno";
}
String result = test(true); // will result in "yes"
解决方案
Because primitive 'true
' will be Autoboxed to Boolean
and which is a Object
.
这篇关于java的:布尔}这种布尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文