开发者

Boolean instanceof Object is true?

开发者 https://www.devze.com 2022-12-21 06:25 出处:网络
I\'ve been learning Java in my spare time and have a quick question I can\'t seem to figure out.This code returns true:

I've been learning Java in my spare time and have a quick question I can't seem to figure out. This code returns 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: unexp开发者_JAVA技巧ected type required: reference found: 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. As the docs say:

The Boolean class wraps a value of the primitive type boolean in an object. 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.

You'll notice that all the primitive types have companion wrapper classes (e.g., int and java.lang.Integer, etc.)

0

精彩评论

暂无评论...
验证码 换一张
取 消