java – boolean. Isinstance (true) is false?

problem

Class objects of Boolean fields do not recognize other Boolean values as instances

code

public class Test {
    public boolean b;
    public static void main(String[] args) {
        System.out.println(Test.class.getFields()[0].getType().isinstance(true));
    }
}

result

false

Solution

The method is isinstance (object), so

x.isinstance(true)

It's the same

x.isinstance(Boolean.TRUE);

And Boolean Class is not Boolean Class

Note: the primitive has no getClass () method or any method When you can use it as an object, it is boxed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>