简体   繁体   English

布尔instanceof对象是否为真?

[英]Boolean instanceof Object is true?

I've been learning Java in my spare time and have a quick question I can't seem to figure out. 我业余时间一直在学习Java,但有一个快速的问题似乎无法解决。 This code returns true: 这段代码返回true:

Boolean testBool = true;
Boolean test = testBool instanceof Object;
System.out.println(test);

However, I thought Boolean was a primitive type and when I try this same logic with any other primitive type I get a compiler error that says: unexpected type required: reference found: int 但是,我以为布尔值是一种原始类型,当我将这种逻辑与其他任何原始类型进行尝试时,都会遇到编译器错误,指出:意外类型要求:找到的引用:int

I'm sure there's just something small I'm missing. 我确定我只缺少一些小东西。 Thanks for your help! 谢谢你的帮助!

Boolean with uppercased initial B wraps a boolean primitive. Boolean与大写初始B 包装一个boolean原语。 As the docs say: 正如文档所说:

The Boolean class wraps a value of the primitive type boolean in an object. 布尔类将原始类型boolean的值包装在对象中。 An object of type Boolean contains a single field whose type is boolean. 布尔类型的对象包含一个布尔类型的字段。

Autoboxing can implicitly move between such boxed types and the corresponding primitives. 自动装箱可以在此类装箱的类型和相应的图元之间隐式移动。

boolean is a primitive type; 布尔值是原始类型; java.lang.Boolean is its wrapper class. java.lang.Boolean是其包装器类。

You'll notice that all the primitive types have companion wrapper classes (eg, int and java.lang.Integer, etc.) 您会注意到所有原始类型都有配套的包装器类(例如int和java.lang.Integer等)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM